dplyr group_by对函数中的变量抛出错误

Cat*_*adu 4 group-by r posixct dplyr posixlt

我使用的是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)

但我仍然有同样的错误.

有谁知道为什么它会在函数中甚至没有的变量上抛出错误?我该如何解决?

akr*_*run 8

所述POSIXlt类被创建在故障dplyr链,因为它是一个不支持classdplyr

d %>% 
   group_by(DatesRoundChar)
Run Code Online (Sandbox Code Playgroud)

grouped_df_impl(data,unname(vars),drop)出错:列 DatesRound:不支持的类POSIXlt/POSIXt

traceback()
#14: stop(list(message = "Column `DatesRound`: unsupported class POSIXlt/POSIXt", 
#        call = grouped_df_impl(data, unname(vars), drop), cppstack = NULL))
#13: .Call("dplyr_grouped_df_impl", PACKAGE = "dplyr", data, symbols, 
#        drop)
#12: grouped_df_impl(data, unname(vars), drop)
#11: grouped_df(groups$data, groups$group_names)
#10: group_by.data.frame(., DatesRoundChar)
#9: group_by(., DatesRoundChar)
#8: function_list[[k]](value)
#7: withVisible(function_list[[k]](value))
#6: freduce(value, `_function_list`)
#5: `_fseq`(`_lhs`)
#4: eval(expr, envir, enclos)
#3: eval(quote(`_fseq`(`_lhs`)), env, env)
#2: withVisible(eval(quote(`_fseq`(`_lhs`)), env, env))
#1: d %>% group_by(DatesRoundChar)
Run Code Online (Sandbox Code Playgroud)

相反,我们可以将其更改为POSIXctas.POSIXct

d$DatesRound <- as.POSIXct(round(as.double(d$Dates)/(15*60))*
                   (15*60),origin=(as.POSIXlt('1970-01-01')))
Run Code Online (Sandbox Code Playgroud)

或者另一个选择是删除之前的'DatesRound'列 group_by

d %>% 
  select(-DatesRound) %>% 
  group_by(DatesRoundChar) %>%
  summarise(total=sum(values))
Run Code Online (Sandbox Code Playgroud)