[R] problems with by()

Heberto Ghezzo heberto.ghezzo at mcgill.ca
Wed Jan 29 22:28:05 CET 2003


Hello, another problem.
 > x<-rep(1,10)
 > y<-rep(c(1,2),c(5,5))
 > z<-seq(1:10)
 > ab<-data.frame(x,y,z)
#
    now I want to do some work by the value of 'y'
 > by(ab,y,mean)
y: 1
x y z
1 1 3
------------------------------------------------------------
y: 2
x y z
1 2 8
#
    I do not want all the means, only the mean of 'z'
 > by(ab,y,function(x) mean(z))
y: 1
[1] 5.5
------------------------------------------------------------
y: 2
[1] 5.5
 > by(ab,y,function(x) mean(z,data=x))
y: 1
[1] 5.5
------------------------------------------------------------
y: 2
[1] 5.5
 >
#
    so, how can I get the function(x) to be applied to each level
of the index variable y.
Actually I use my own function but the same happens, it is applied to all
the data and there is no partition of the data acording to index
Do not tell me that this version of R is completely buggy, I was waiting
for the 1.7 to be out before upgrading

R : Copyright 2002, The R Development Core Team
Version 1.6.1  (2002-11-01)

R.Heberto.Ghezzo Ph.D.
Meakins-Christie Labs
McGill University
Montreal - Canada




More information about the R-help mailing list