Hi all, May I request for your help if you have time and if you have an idea on how to do this. I want to add three vectors... And my goal is to obtain the sum of the vectors, ignoring the vector of "na"... Here is what i did in R.. I'm adding the three vectors, e,z,k, and my objective is to get an answer = -23. I tried putting the na.omit but it did not work. Thanks.> z[1] -12 -9> e[1] -2 0> k[1] NA NA> sum(z+e+k)[1] NA>-- View this message in context: http://n4.nabble.com/Summing-with-NA-tp1678564p1678564.html Sent from the R help mailing list archive at Nabble.com.
k <- c(NA, NA) z <- c(-12, -9) e <- c(-2,0) sum(na.omit(c(z, e, k))) Will this do the trick? Why are you summing in a sum() sum(k+z+e) On Mon, Mar 22, 2010 at 8:51 PM, tj <girlme80 at yahoo.com> wrote:> > Hi all, > May I request for your help if you have time and if you have an idea on how > to do this. ?I want to add three vectors... And my goal is to obtain the sum > of the vectors, ignoring the vector of "na"... > > Here is what i did in R.. I'm adding the three vectors, e,z,k, and my > objective is to get an answer = -23. > I tried putting the na.omit but it did not work. ?Thanks. > >> z > [1] -12 ?-9 >> e > [1] -2 ?0 >> k > [1] NA NA >> sum(z+e+k) > [1] NA >> > > -- > View this message in context: http://n4.nabble.com/Summing-with-NA-tp1678564p1678564.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
On Mar 22, 2010, at 9:51 PM, tj wrote:> > Hi all, > May I request for your help if you have time and if you have an idea > on how > to do this. I want to add three vectors... And my goal is to obtain > the sum > of the vectors, ignoring the vector of "na"... > > Here is what i did in R.. I'm adding the three vectors, e,z,k, and my > objective is to get an answer = -23. > I tried putting the na.omit but it did not work. Thanks. > >> z > [1] -12 -9 >> e > [1] -2 0 >> k > [1] NA NA >> sum(z+e+k) > [1] NA?sum note na.rm and do not use "+". > sum(z, e, k, na.rm=T) [1] -23>>David Winsemius, MD West Hartford, CT
Slightly longer method, but works as well. z <- c(-12,-9) e <- c(-2,0) k <- c(NA,NA) x <- c(z,e,k) x1 <- which(x!="NA",arr.ind=TRUE) # get elements which are not NA x2 <- x[x1] sum(x2) [1] -23 -- Muhammad tj wrote:> Hi all, > May I request for your help if you have time and if you have an idea on how > to do this. I want to add three vectors... And my goal is to obtain the sum > of the vectors, ignoring the vector of "na"... > > Here is what i did in R.. I'm adding the three vectors, e,z,k, and my > objective is to get an answer = -23. > I tried putting the na.omit but it did not work. Thanks. > > >> z >> > [1] -12 -9 > >> e >> > [1] -2 0 > >> k >> > [1] NA NA > >> sum(z+e+k) >> > [1] NA > > >