我正在尝试重现此博客文章中针对by()函数的分析。但是,当我将代码粘贴到RI中时,会收到一条错误消息,而不是博客文章中漂亮的汇总虹膜数据表。
attach(iris)
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
这样数据帧就在那里了,一切都很好。
从博客中粘贴by()函数会给我这个错误:
by(iris[, 1:4], Species, mean)
Species: setosa
[1] NA
----------------------------------------------------------------------------------
Species: versicolor
[1] NA
----------------------------------------------------------------------------------
Species: virginica
[1] NA
Warning messages:
1: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
2: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(data[x, , drop = FALSE], ...) :
argument is not numeric or logical: returning NA
我真的看不到这里有什么问题。我已经尝试过使用其他数据帧,依此类推,问题似乎出在数据帧索引的1:4序列上。如果仅指定一列,则表示没有问题。我无法弄清楚为什么在给多列的情况下它会吐出它的假人。有什么建议?
我不确定博客文章的年龄,但是如果我查看的文档by
,其功能将与博客文章所描述的不同。
by
将输入数据拆分为子集的数据框,但您无法获得一个数据框mean
!
mean(iris[,1:4])
[1] NA
Warning message:
In mean.default(iris[, 1:4]) :
argument is not numeric or logical: returning NA
by
如果要mean
在一列中获取值,可以使用,
by(iris[,1], iris$Species, mean)
iris$Species: setosa
[1] 5.006
---------------------------------------------------------------------------------------------
iris$Species: versicolor
[1] 5.936
---------------------------------------------------------------------------------------------
iris$Species: virginica
[1] 6.588
但是要获取所有列的均值,请aggregate
按照@Thomas的建议使用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句