与lapply和dplyr / magrittr有奇怪的错误 [英] Weird error with lapply and dplyr/magrittr

查看:140
本文介绍了与lapply和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)必须有一个正长度?他们似乎与我相同。

Why does the "no dplyr" version work, and the "dplyr" version throws the error, "dim(X) must have a positive length"? They seem identical to me.

推荐答案

问题是您还没有完全实现管道。您正在使用 magrittr ,而且这个问题与 dplyr

The issue is that you haven't quite fully implemented the pipe line. You are using magrittr here, and the issue has little to do with dplyr

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

这篇关于与lapply和dplyr / magrittr有奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆