lapply 和 dplyr/magrittr 的奇怪错误

Weird error with lapply and dplyr/magrittr

这是一段代码:

data <- data.frame(a=runif(20),b=runif(20),subject=rep(1:2,10)) %>%
group_by(subject) %>%
do(distance = dist(.))

#no dplyr
intermediate <- lapply(data$distance,as.matrix)
mean.dists <- apply(simplify2array(intermediate),MARGIN = c(1,2),FUN=mean)

#dplyr
mean.dists <- lapply(data$distance,as.matrix) %>%
apply(simplify2array(.),MARGIN=c(1,2),FUN=mean)

为什么 "no dplyr" 版本有效,而 "dplyr" 版本抛出错误,"dim(X) must have a positive length"?他们看起来和我一模一样。

问题是您还没有完全实现流水线。你这里用的是magrittr,和dplyr

关系不大
data$distance %>% 
   lapply(as.matrix ) %>% 
   simplify2array %>% 
   apply(MARGIN=1:2, FUN=mean)