我使用的是R 3.4.0和dplyr 0.5.0(我也使用R 3.3.3测试过,我也有同样的错误).
我过去经常使用以下类型的代码(甚至昨天!)但由于某些原因它今天会产生错误.
例如,我有5分钟的间隔数据,我希望在15分钟内总结一下.由于我不能使用group_byDateTime POSIXlt,因此我将变量转换为字符.但是,当我应用该group_by函数时,它会在原始DateTime POSIXlt变量上创建一个错误,即使我在函数中使用了字符变量.
这是一个可重复的例子:
z <- seq(ISOdatetime(2017,01,01, 00,00,00), ISOdatetime(2017,02,28,23,45,00), by="5 min")
q <- rnorm(16990, mean=120, sd=75)
d<- data.frame("Dates"=z, "values"=q)
# Round the time to the nearest 15min
d$DatesRound <- as.POSIXlt(round(as.double(d$Dates)/(15*60))*(15*60),origin=(as.POSIXlt('1970-01-01')))
# Transform into character
d$DatesRoundChar <- as.character(d$DatesRound)
d2 <-
d %>%
group_by(DatesRoundChar)%>%
summarise(total=sum(values))
Run Code Online (Sandbox Code Playgroud)
这是我的错误:
grouped_df_impl(data,unname(vars),drop)出错:列'DatesRound'有不支持的类:POSIXlt,POSIXt
我也尝试使用以下方法进行转换:
d$DatesRoundChar <- strftime(as.POSIXct(d$DatesRound))
d$DatesRoundChar <- sapply(d$DatesRound, as.character)
Run Code Online (Sandbox Code Playgroud)
但我仍然有同样的错误.
有谁知道为什么它会在函数中甚至没有的变量上抛出错误?我该如何解决?