[R] Adding two or more columns of a data frame for each row when NAs are present.

Ian Strang hamamelis at ntlworld.com
Tue Nov 22 21:39:27 CET 2011


I think, here is the solution. If NA is included in read.table list the row 
becomes a factor:
$ Q21: Factor w/ 3 levels " 1"," 2"," NA": 1 2 3 2 2. This will not work 
with rowSums.
If I put the missing value as a blank, then it is still read as NA but the 
whole row is considered as an integer and OK for rowSums etc.
A missing value in col Q00 will be interpreted as a row with one less value 
at the end, I think.
Ian

+ yy <- read.table( header = T, sep=",", text =
+ "Q00, Q20, Q21, Q22, Q23, Q24
+  0, 0, 1, 2, 3, 4
+  0, 1, 2, 3, 4, 5
+  0, 0, NA, 3, 4, 5
+  0, 1, 2, 3, 4, 5
+  0, 1, 2, 3, 4, 5")
+  yy
   Q00 Q20 Q21 Q22 Q23 Q24
1   0   0   1   2   3   4
2   0   1   2   3   4   5
3   0   0  NA   3   4   5
4   0   1   2   3   4   5
5   0   1   2   3   4   5
+ str(yy)
'data.frame': 5 obs. of  6 variables:
  $ Q00: int  0 0 0 0 0
  $ Q20: int  0 1 0 1 1
  $ Q21: Factor w/ 3 levels " 1"," 2"," NA": 1 2 3 2 2
  $ Q22: int  2 3 3 3 3
  $ Q23: int  3 4 4 4 4
  $ Q24: int  4 5 5 5 5

+ yy <- read.table( header = T, sep=",", text =
+ "Q00, Q20, Q21, Q22, Q23, Q24
+  0, 0, 1, 2, 3, 4
+  0, 0, , 3, 4, 5
+  0, 1, 2, 3, 4, 5")
+  yy
   Q00 Q20 Q21 Q22 Q23 Q24
1   0   0   1   2   3   4
2   0   0  NA   3   4   5
3   0   1   2   3   4   5
+ str(yy)
'data.frame': 3 obs. of  6 variables:
  $ Q00: int  0 0 0
  $ Q20: int  0 0 1
  $ Q21: int  1 NA 2
  $ Q22: int  2 3 3
  $ Q23: int  3 4 4
  $ Q24: int  4 5 5
+ x <- transform( yy,                                        ############ 
Example 6
+    mySum   = rowSums(yy[ ,c("Q00","Q20","Q21","Q23")], na.rm=T),
+    myCount = 
as.numeric(!is.na(Q20))+as.numeric(!is.na(Q21))+as.numeric(!is.na(Q24)),
+    myMean  = rowMeans(yy[ ,c("Q00","Q20","Q21","Q23")], na.rm=T)
+  )
+ x
   Q00 Q20 Q21 Q22 Q23 Q24 mySum myCount   myMean
1   0   0   1   2   3   4     4       3 1.000000
2   0   0  NA   3   4   5     4       2 1.333333
3   0   1   2   3   4   5     7       3 1.750000



More information about the R-help mailing list