lun zhang lun zhang - 3 months ago 8
R Question

P146 example from the book "R in action"

From book "R in action" page 146 it has an 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 I input this into R what I get is

> 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


I do not know what happens here. Can anyone gives me a help. Thanks.

Pj_ Pj_
Answer

Please do ?by() in your R Console.

In the help, you'll find the following mentioned for the FUN

by(data, INDICES, FUN, ..., simplify = TRUE)

FUN a function to be applied to (usually data-frame) subsets of data.

Update

This probably more has to do with the way mean() and sd() are defined. They both on support vector, not data frames.

Checkout the examples below, and see the difference that comes with mean() with and without Vectorize():

x <- data.frame( a = c(1, 2, 4), b = c(1, 2, 4))

Now, if you do:

by(x, 1:3, mean)

You will get the following error:

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

But upon adding Vectorize():

by(x, 1:3, Vectorize(mean))
1:3: 1
a b 
1 1 
------------------------------------------------------- 
1:3: 2
a b 
2 2 
------------------------------------------------------- 
1:3: 3
a b 
4 4 

Also from the forums:

by(as.data.frame(mtcars[vars]), mtcars$am, dstats)

The problem is with the by function. If you replace dstats with mean or sd, it still does not work. It only works with the summary function. I wonder how the author got the output in the book?

Source: https://forums.manning.com/posts/list/32059.page

Comments