Finding the sum of repeated continuous variables for each subject
Suppose you have a data set with repeated measures --- i.e., you have multiple values of a continuous variable (e.g.,
var1
and
var2
below) for each subject in your data set (each subject could have a different number of records).
d = data.frame(d=c(1,1,1,2,2,4,4,4), var1 = round(rnorm(8),1 ),
var2 = c(round(rnorm(8),1)))
d
You would like to find the sum of the repeated
var1
and
var2
values for each subject and you would like to keep these results in your original repeated measures data frame.
Here's the 'solution:'
varList = c("var1", "var2")
for (n in varList){
t = tapply(dN, d$id, sum, na.rm=TRUE)
dPaste(Sum,n,sep=) = t[as.character(d$id)]
}
d
This problem and solution were posted by Svetlana Eden.