2018-11-09
阅读量:
879
R中关于by的一个问题
> dstats <- function(x)(c(mean=mean(x), sd=sd(x)))
> by(mtcars[vars],mtcars$am, dstats)
Error in is.data.frame(x) :
(list) object cannot be coerced to type 'double'
In addition: Warning message:
In mean.default(x) : argument is not numeric or logical: returning NA
我看R语言实战上面也这么写的。能运行出结果,后来翻阅书籍帮我解决了这个,参考如下:
mean()函数只支持向量,不支持数据框输入。所以自定义函数dstats也不支持数据框输入。
解决办法是将dstats改成如下,就可以了:dstats <- function(x) c(mean=sapply(x,mean), sd=sapply(x,sd))是sd=sapply(x,sd)






评论(0)


暂无数据
推荐帖子
0条评论
0条评论
1条评论