[R] How to join data.frames and vectors of different length, in an inteligent way?

Hvidberg, Martin mhv at dmu.dk
Wed Jun 11 08:52:41 CEST 2008


Thanks Chuck

With your help I managed to write the code as I wanted it.
The result looks like this:

dafSamp <-
data.frame(cbind(c(1972,1984,1969,1976,1999,1996,1976,1984,1976),c(117,7
3,92,113,80,78,98,106,99)))
dafSamp$Ay <- ave(dafSamp$X2, dafSamp$X1, FUN=mean)
dafSamp$AA <- dafSamp$X2 * (mean(dafSamp$X2)/dafSamp$Ay)
dafSamp$My <- ave(dafSamp$X2, dafSamp$X1, FUN=median)
dafSamp$MA <- dafSamp$X2 * (median(dafSamp$X2)/dafSamp$My)
par(mfrow=c(1,2))
boxplot(AA~X1, data=dafSamp, main="Mean mode")
boxplot(MA~X1, data=dafSamp, main="Median mode")

It works like a dream.Thanks for you time
Martin



More information about the R-help mailing list