The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

cumin (1) [Avatar] Offline
#1
the book shows this:

> dstats <- function(x)(c(mean=mean(x), sd=sd(x)))
> by(mtcars[vars], mtcars$am, dstats)

mtcars$am: 0
mean.mpg mean.hp mean.wt sd.mpg sd.hp sd.wt
17.147 160.263 3.769 3.834 53.908 0.777
------------------------------------------------
mtcars$am: 1
mean.mpg mean.hp mean.wt sd.mpg sd.hp sd.wt
24.392 126.846 2.411 6.167 84.062 0.617



When I try it, I get this:
> dstats <- function(x)(c(mean=mean(x), sd=sd(x)))
> by(mtcars[vars], mtcars$am, mean)
mtcars$am: 0
[1] NA
-----------------------------------------------
mtcars$am: 1
[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
ckangai (2) [Avatar] Offline
#2
Re: Listing 7,7 Descriptive statisitcs by group using "by()"
Listing 7.7 certainly does not work. Accessing help via help(by) suggests it should work, but it does not. It works only if the data frame is one column. For example, the following works:
by(mtcars[,1], mtcars$am, dstats) #or by(mtcars[,2], mtcars$am, dstats)
I have tried coercing the first argument into a data frame since the error messages suggests it is being passed a list, and still it does not work. i.e. the following does not work

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?

Thanks in advance.
Charles Kangai (Bristol, UK)