[R] Summing up columns with NA elements in a data frame

ravi rv15i at yahoo.se
Tue Sep 23 17:20:45 CEST 2008


Hi all,
When I try to sum a few columns in a data frame (which has NA’s randomly distributed as elements), I am not able to get the summing operation to ignore the NA’s.
Let me explain with the following code :
################################ 
a<-structure(list(a1 = c(NA, 2, 3, NA, 5, 6, NA, 8, 9, NA), a2 = c(NA,
+ 4, 5, 5, NA, 7, 8, 8, NA, 10), a3 = c(2, NA, 6, 8, NA, 12, 14,
+ NA, 18, 20)), .Names = c("a1", "a2", "a3"), row.names = c(NA,
+ -10L), class = "data.frame")
print(a)
a$a4<-a$a1+a$a2+a$a3 #method 1
print(a)
b<-apply(a,1,sum) #method 2
print(b)
d<-apply(a,1,FUN=function(x) (sum(x),na.rm=T)) # not the right syntax, apparently - method 3
print(d)
###################################### 
I would like to know how to make the na.rm=T to work, or any other solution. 
I am also mystified by the following result :
 ####################################
attach(a)
a$a4<-a1+a2+a3 # method 4
detach(a)
print(a)
############################# 
Now, the row numbers (why?) seem to get added - sometimes.
There are some things that I am missing here. Will appreciate all help in understanding the issues here.
Thanking You,
Ravi



More information about the R-help mailing list