r - aggregate() 和 ave() 与日期组合的问题
问题描述
aggregate()
如果我将和与日期(格式)结合ave()
在同一个函数中,我会遇到一些问题:2020-03-16
#Raw nest data
raw.Atta<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/cres_ninho_ed.csv", sep=";", h=T)
str(raw.Atta)
## Create the days in each class
Atta.db.1 <- merge(raw.Atta,
within(
aggregate(data ~ ninho + classe, raw.Atta, max),
step3 <- ave(data, ninho, FUN = function(x) diff(c(0, x)))
),
by = c("ninho", "classe"),
all = TRUE
)
Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] :
non-numeric argument to binary operator error
我将日期更改为另一种格式:
raw.Atta <- raw.Atta %>%
mutate(date = as.POSIXlt(data, format = "%Y-%m-%d")) # convert to datetime object
Error in model.frame.default(formula = data ~ ninho + classe, data = raw.Atta) :
产生新问题!!
请一些想法?
提前致谢!
解决方案
尝试这个。这个问题的根源在于diff()
它需要一个日期变量,而你的则是一个字符:
#Raw nest data
raw.Atta<- read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/cres_ninho_ed.csv", sep=";", h=T)
str(raw.Atta)
## Create the days in each class
Atta.db.1 <- merge(raw.Atta,
within(
aggregate(data ~ ninho + classe, raw.Atta, max),
step3 <- ave(data, ninho, FUN = function(x) diff(c(0, as.Date(x))))
),
by = c("ninho", "classe"),
all = TRUE
)
推荐阅读
- hadoop - 是否建议使用 Hadoop 生态系统并行处理来管理和维护缓慢变化的数据仓库维度?
- javascript - 如何将表单中的输入中的多个值推送到 mongoose.model 中的对象数组?
- php - PHP中不推荐使用的每个函数的替代解决方案
- python - 使用 Firefox WebDriver 单击与同名标签耦合的单选按钮
- android - 当应用不在屏幕顶部时保持活动
- dart - Dart:无法推断函数文字的类型,因为文字有一个块作为其主体
- html - 如何使具有下拉菜单的导航栏居中
- android - 使用低 SDK 构建工具版本编译
- python - 如何检查它是否有一个类正在运行?
- java - Orika 运行时的可选转换/映射