我在使用dplyr时遇到了一个有趣的错误。
Error in summarise_impl(.data, dots) :
Column `x_sum` must be length 1 (a summary value), not 10
我创建了一个示例并尝试修复它。修复程序显示在下面的代码中,它可以工作。但是,当我在真正的DF上使用它时会产生错误。有任何想法吗?以下所有代码。
我尝试了rowwise()nil luck(产生了所有行)。我试过变异,但也没有运气。
loc <- c("aaa","bbb","ccc","aaa","bbb","ccc")
wk <- c("5","5","6","6","7","7")
a <- c("A","B","C","D","WKSH","WKSH")
b <- c(10,11,12,13,14,15)
c <- c(10,11,12,13,14,15)
d <- c(10,11,12,13,14,15)
e <- c(10,11,12,13,14,15)
x <- c(0,1,0,0,5,5)
df <- data.frame(loc,wk,a,b,c,d,e,x)
Summary <- df %>%
group_by(loc, wk) %>%
summarise(b_sum = sum(b), c_sum = sum(c), d_sum = sum(d), e_sum = sum(e), x_sum = ifelse(a == "WKSH", sum(x),0) ) %>%
mutate(SumAll = b_sum + c_sum + d_sum + e_sum)