[R] using "mean" in by(x,y,mean)

William Revelle lists at revelle.net
Wed Jul 14 22:45:21 CEST 2004


Dear list friends

I fail to understand how to find means for multiple groups using the 
by() function. Help would be appreciated. Thanks.
Bill


x <- runif(20,0,10)
group <- rep(c("A","B"),10)
df <-data.frame(x,group)
#df            #show the data

rm(x,group)
attach(df)

sd(x)       # sd is defined
mean(x)     #so is mean

by(x,group,sd)   #this works for both groups
by(x,group,mean)  #this does not


produces this output:


>  x <- runif(20,0,10)
>  group <- rep(c("A","B"),10)
>  df <-data.frame(x,group)
>  #df            #show the data
>
>  rm(x,group)
>  attach(df)
>
>  sd(x)       # sd is defined
[1] 2.952699
>  mean(x)     #so is mean
[1] 5.026441
>
>  by(x,group,sd)   #this works for both groups
INDICES: A
[1] 2.813504
------------------------------------------------------------------------------------------------------------ 

INDICES: B
[1] 3.236663
>  by(x,group,mean)  #this does not
Error in FUN(X[[as.integer(1)]], ...) : couldn't find function "FUN"


I am using a Mac OS 10.3.4 running R 1.9.1
-- 
----------------------------
William Revelle               http://pmc.psych.northwestern.edu/revelle.html
Department of Psychology, Northwestern University
Personality Project: http://personality-project.org/personality.html




More information about the R-help mailing list