<br />
vars <- c("mpg", "hp", "wt")<br />
dstats <- function(x)(c(mean=mean(x), sd=sd(x)))<br />
by(mtcars[vars], mtcars$am, dstats)<br />
运行出现下列问题:
<br />
> vars <- c("mpg", "hp", "wt")<br />
> dstats <- function(x)(c(mean=mean(x), sd=sd(x)))<br />
> by(mtcars[vars], mtcars$am, dstats)<br />
Error in is.data.frame(x) :<br />
(list) object cannot be coerced to type 'double'<br />
In addition: Warning message:<br />
In mean.default(x) : argument is not numeric or logical: returning NA<br />
</p>
不知道为什么,谢谢。
另外,在146页有一段话
Unfortunately, aggregate() only allows you to use single value functions such as
mean, standard deviation, and the like in each call. It won’t return several statistics at once. For that task, you can use the by() function.
但是我发现
<br />
vars <- c("mpg", "hp", "wt")<br />
dstats <- function(x)(c(mean=mean(x), sd=sd(x)))<br />
aggregate(mtcars[vars], by = list(am = mtcars$am), dstats)<br />
却能运行,并且有以下结果。
</p>
<p>> vars <- c("mpg", "hp", "wt")<br />
> dstats <- function(x)(c(mean=mean(x), sd=sd(x)))<br />
> aggregate(mtcars[vars], by = list(am = mtcars$am), dstats)<br />
am mpg.mean mpg.sd hp.mean hp.sd wt.mean<br />
1 0 17.147368 3.833966 160.26316 53.90820 3.7688947<br />
2 1 24.392308 6.166504 126.84615 84.06232 2.4110000<br />
wt.sd<br />
1 0.7774001<br />
2 0.6169816<br />
</p>
是不是作者解释错了。