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
Henrique Dallazuanna
2008-Sep-23 15:45 UTC
[R] Summing up columns with NA elements in a data frame
Try this: a$a4 <- rowSums(a, na.rm = T) On Tue, Sep 23, 2008 at 12:20 PM, ravi <rv15i at yahoo.se> wrote:> 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 > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O