I'm trying to add a column of the means to my data-frame with some code I can run over many different columns even if they include NA's (which I think excludes many possibilities).
The best I could do is:
TestData <- data.frame(geo=c(rep("AT",4),rep("DE",4)),time=c(rep(c(1990:1993),2)),value=c(NA,4,20,6,NA,NA,5,3))
mean <- aggregate(value~geo, TestData, mean)
which calculates the correct means by category (geo). How can I rightjoin them to the dataframe so that the mean is not just one observation but shows up at every time point? I'm thinking about ddply but can't get it to work properly. The data frame I'm looking for is:
geo time value mean (or optionally, no problem for me)
1 AT 1990 NA 10 NA
2 AT 1991 4 10 10
3 AT 1992 20 10 10
4 AT 1993 6 10 10
5 DE 1990 NA 4 NA
6 DE 1991 NA 4 NA
7 DE 1992 5 4 4
8 DE 1993 3 4 4
Any help would be appreciated!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…