I have a really simple R question but I can\'t seem to find an adequate solution. Let\'s say we have the following data frame:
groupid<-rep(1:5, each=3)
n
testscores$testMean <- rowMeans(testscores[,3:4], na.rm=TRUE)
> testscores
groupid names test1 test2 testMean
1 1 Bill 90 80 85.0
2 1 Jim 70 NA 70.0
3 1 Sarah 90 92 91.0
4 2 Mike NA 80 80.0
5 2 Jennifer 100 65 82.5
6 2 Bill 90 80 85.0
7 3 Jim 70 NA 70.0
8 3 Sarah 90 92 91.0
9 3 Mike NA 80 80.0
10 4 Jennifer 100 65 82.5
11 4 Bill 90 80 85.0
12 4 Jim 70 NA 70.0
13 5 Sarah 90 92 91.0
14 5 Mike NA 80 80.0
15 5 Jennifer 100 65 82.5
you can also use this
testscores <- structure(list(groupid = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 5L, 5L, 5L), names = structure(c(1L, 3L, 5L, 4L,
2L, 1L, 3L, 5L, 4L, 2L, 1L, 3L, 5L, 4L, 2L), .Label = c("Bill",
"Jennifer", "Jim", "Mike", "Sarah"), class = "factor"), test1 = c(90,
70, 90, NA, 100, 90, 70, 90, NA, 100, 90, 70, 90, NA, 100), test2 = c(80,
NA, 92, 80, 65, 80, NA, 92, 80, 65, 80, NA, 92, 80, 65)), .Names = c("groupid",
"names", "test1", "test2"), row.names = c(NA, -15L), class = "data.frame")
testscores$meanTest=rowMeans(testscores[,c("test1", "test2")], na.rm=TRUE)
# groupid names test1 test2 meanTest
#1 1 Bill 90 80 85.0
#2 1 Jim 70 NA 70.0
#3 1 Sarah 90 92 91.0
#4 2 Mike NA 80 80.0
#5 2 Jennifer 100 65 82.5
#6 2 Bill 90 80 85.0
#7 3 Jim 70 NA 70.0
#8 3 Sarah 90 92 91.0
#9 3 Mike NA 80 80.0
#10 4 Jennifer 100 65 82.5
#11 4 Bill 90 80 85.0
#12 4 Jim 70 NA 70.0
#13 5 Sarah 90 92 91.0
#14 5 Mike NA 80 80.0
#15 5 Jennifer 100 65 82.5