我遇到了一些问题,dplyr返回错误“index out of bounds”。
似乎只有长变量名才会发生。显然,这可以通过重命名有问题的变量来解决,但我想把它放在这里,以帮助其他人解决相同的问题,因为返回的错误不提供信息。
> test <- data.frame(name = rep(c("orange", "pear", "apple", "bear"), 2) ,
+ value = rnorm(8), stringsAsFactors = FALSE)
>
> group_by(test, name) %>% summarise(mean(value))
Source: local data frame [4 x 2]
name mean(value)
1 apple -0.34881128
2 bear 1.01526013
3 orange -0.68778455
4 pear -0.06635953
>
> test$reallyreallyreallyreallyreallylongvarname <- test$name
> group_by(test, reallyreallyreallyreallyreallylongvarname) %>% summarise(mean(value))
Error in eval(expr, envir, enclos) : index out of bounds
答案 0 :(得分:3)
可能的解决方法是在quote()
内使用group_by()
。
> group_by(test, quote(reallyreallyreallyreallyreallylongvarname)) %>% summarise(mean(value))
Source: local data frame [4 x 2]
quote(reallyreallyreallyreallyreallyl... mean(value)
1 apple -0.34881128
2 bear 1.01526013
3 orange -0.68778455
4 pear -0.06635953