Answers:
在另一种情况下,以下代码中发生了此错误。
library(dplyr) # dplyr 0.5.0
library(lazyeval)
df <- data_frame(group = c(1, 2, 2, 3, 3, 3))
g <- "group"
df %>%
group_by_(g) %>%
summarise_(
n = n(),
sum = interp(~sum(col, na.rm = TRUE), col = as.name(g))
)
# Error in n() : This function should not be called directly
可以如下解决。
df %>%
group_by_(g) %>%
summarise_(
n = "n()",
sum = interp(~sum(col, na.rm = TRUE), col = as.name(g))
)
# A tibble: 3 × 3
# group n sum
# <dbl> <int> <dbl>
# 1 1 1 1
# 2 2 2 4
# 3 3 3 9
按照提到的博客执行代码,然后在detach(“ package:plyr”,unload = TRUE)中运行解决方案时遇到类似的问题
博客:https : //www.analyticsvidhya.com/blog/2017/09/comparative-stock-analysis/
Master_Data_AutoCorrelations<-Master_Data_lags %>%
gather(key = "lag", value = "lag_value", -c(Stock,Date, Close)) %>%
mutate(lag = str_sub(lag, start = 5) %>% as.numeric) %>%
group_by(Stock, lag) %>%
summarize(
cor = cor(x = Close, y = lag_value, use = "pairwise.complete.obs"),
cutoff_upper = 2/(n())^0.5,
cutoff_lower = -2/(n())^0.5
)
发布运行分离后,重新运行上述代码后,尽管收到如下警告消息,但仍能正常工作,不确定plyr是否已卸载。如何正确执行代码?
警告消息:“ plyr”名称空间无法卸载:名称空间“ plyr”由“ reshape2”,“ scales”,“ broom”,“ ggplot2”导入,因此无法卸载
对我来说,解决方案就是detach()
功能,我利用了该功能
plyr
先加载