P146 example from the book "R in action" -
from book "r in action" page 146 has example of group descriptive statistics by
vars <- c("mpg", "hp", "wt") ... > dstats <- function(x)(c(mean=mean(x), sd=sd(x))) > by(mtcars[vars], mtcars$am, dstats)
but when input r
> error in is.data.frame(x) : >(list) object cannot coerced type 'double' >in addition: warning message: >in mean.default(x) : argument not numeric or logical: returning na
i not know happens here. can gives me help. thanks.
please ?by()
in r console.
in help, you'll find following mentioned fun
by(data, indices, fun, ..., simplify = true) fun function applied (usually data-frame) subsets of data.
update
this more has way mean() , sd() defined. both on support vector, not data frames.
checkout examples below, , see difference comes mean()
, without vectorize()
:
x <- data.frame( = c(1, 2, 4), b = c(1, 2, 4))
now, if do:
by(x, 1:3, mean)
you following error:
warning messages: 1: in mean.default(data[x, , drop = false], ...) : argument not numeric or logical: returning na 2: in mean.default(data[x, , drop = false], ...) : argument not numeric or logical: returning na 3: in mean.default(data[x, , drop = false], ...) : argument not numeric or logical: returning na
but upon adding vectorize()
:
by(x, 1:3, vectorize(mean)) 1:3: 1 b 1 1 ------------------------------------------------------- 1:3: 2 b 2 2 ------------------------------------------------------- 1:3: 3 b 4 4
also forums:
by(as.data.frame(mtcars[vars]), mtcars$am, dstats)
the problem function. if replace dstats mean or sd, still not work. works summary function. wonder how author got output in book?
Comments
Post a Comment