Petra Opic
2012-Jan-10 16:25 UTC
[R] Sum of a couple of variables of which a few have NA values
Dear everyone, I have looked all over the internet but I cannot find a way to solve my problem. In my data I want to sum a couple of variables. Some of these variables have NA values, and when I add them together, the result is NA dat <- data.frame( id = gl(5,1), var1 = rnorm(5, 10), var2 = rnorm(5, 7), var3 = rnorm(5, 6), var4 = rnorm(5, 3), var5 = rnorm(5, 8) ) dat[3,3] <- NA dat[4,5] <- NA> dat?id ? ? ?var1 ? ? var2 ? ? var3 ? ? var4 ? ? var5 1 ?1 ?9.371328 7.830814 5.032541 3.491053 7.626418 2 ?2 10.413516 7.333630 6.557178 1.465597 8.591770 3 ?3 10.967073 ? ? ? NA 6.674079 3.946451 7.251263 4 ?4 ?9.900380 7.727111 5.059698 ? ? ? NA 6.632962 5 ?5 ?9.191068 7.901271 6.652410 2.734856 8.484757 attach(dat) dat$sum <- var2 + var3 + var4 # I think I'm doing this wrong, but I don't know what command to use> datid var1 var2 var3 var4 var5 sum 1 1 9.371328 7.830814 5.032541 3.491053 7.626418 16.35441 2 2 10.413516 7.333630 6.557178 1.465597 8.591770 15.35640 3 3 10.967073 NA 6.674079 3.946451 7.251263 NA 4 4 9.900380 7.727111 5.059698 NA 6.632962 NA 5 5 9.191068 7.901271 6.652410 2.734856 8.484757 17.28854 I would like to omit the values of NA and just sum the rest. I tried to use rowSums() but that sums an entire row and I only need a few variables. Does anyone know how to do this? Thanks in advance, Petra
Filoche
2012-Jan-10 16:41 UTC
[R] Sum of a couple of variables of which a few have NA values
x = runif(10) x[4] = NA sum(x, na.rm = T) -- View this message in context: http://r.789695.n4.nabble.com/Sum-of-a-couple-of-variables-of-which-a-few-have-NA-values-tp4282448p4282483.html Sent from the R help mailing list archive at Nabble.com.
Ivan Calandra
2012-Jan-10 16:44 UTC
[R] Fwd: Sum of a couple of variables of which a few have NA values
Hi Petra, Try this: dat$sums <- rowSums(dat[3:5], na.rm=TRUE) I think this should do what you're looking for HTH, Ivan -------- Message original -------- Sujet: [R] Sum of a couple of variables of which a few have NA values Date : Tue, 10 Jan 2012 17:25:21 +0100 De : Petra Opic <petraopic at gmail.com> Pour : r-help at r-project.org Dear everyone, I have looked all over the internet but I cannot find a way to solve my problem. In my data I want to sum a couple of variables. Some of these variables have NA values, and when I add them together, the result is NA dat<- data.frame( id = gl(5,1), var1 = rnorm(5, 10), var2 = rnorm(5, 7), var3 = rnorm(5, 6), var4 = rnorm(5, 3), var5 = rnorm(5, 8) ) dat[3,3]<- NA dat[4,5]<- NA> datid var1 var2 var3 var4 var5 1 1 9.371328 7.830814 5.032541 3.491053 7.626418 2 2 10.413516 7.333630 6.557178 1.465597 8.591770 3 3 10.967073 NA 6.674079 3.946451 7.251263 4 4 9.900380 7.727111 5.059698 NA 6.632962 5 5 9.191068 7.901271 6.652410 2.734856 8.484757 attach(dat) dat$sum<- var2 + var3 + var4 # I think I'm doing this wrong, but I don't know what command to use> datid var1 var2 var3 var4 var5 sum 1 1 9.371328 7.830814 5.032541 3.491053 7.626418 16.35441 2 2 10.413516 7.333630 6.557178 1.465597 8.591770 15.35640 3 3 10.967073 NA 6.674079 3.946451 7.251263 NA 4 4 9.900380 7.727111 5.059698 NA 6.632962 NA 5 5 9.191068 7.901271 6.652410 2.734856 8.484757 17.28854 I would like to omit the values of NA and just sum the rest. I tried to use rowSums() but that sums an entire row and I only need a few variables. Does anyone know how to do this? Thanks in advance, Petra ______________________________________________ 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. -- Ivan CALANDRA Universit? de Bourgogne UMR CNRS 5561 Biog?osciences 6 Boulevard Gabriel 21000 Dijon, FRANCE ivan.calandra at u-bourgogne.fr
David Winsemius
2012-Jan-10 16:48 UTC
[R] Sum of a couple of variables of which a few have NA values
On Jan 10, 2012, at 11:25 AM, Petra Opic wrote:> Dear everyone, > > I have looked all over the internet but I cannot find a way to solve > my problem.? rowSums # has an na.rm argument> > In my data I want to sum a couple of variables. Some of these > variables have NA values, and when I add them together, the result is > NAsnip> attach(dat)You would be well-advised to forget `attach`. Use with(dat, ...) instead. It will prevent frustration and embarrassing postings to rhelp.> > dat$sum <- var2 + var3 + var4The plus infix operator does not have an na.rm argment dat$sum <- rowSums(dat[ , 2:4] , na.rm=TRUE) -- David Winsemius, MD West Hartford, CT
PetraOpic
2012-Jan-10 18:47 UTC
[R] Fwd: Sum of a couple of variables of which a few have NA values
Dear Ivan, Thank you very much for your help. How do I use rowSums if I need to "skip" a variable from summing? (example: sum var1, var2, var3, var5, var34 only). Thanks in advance, Petra Opic -- View this message in context: http://r.789695.n4.nabble.com/Sum-of-a-couple-of-variables-of-which-a-few-have-NA-values-tp4282448p4282969.html Sent from the R help mailing list archive at Nabble.com.