John Kane
2006-May-01 17:26 UTC
[R] Adding elements in an array where I have missing data.
This is a simple question but I cannot seem to find the answer. I have two vectors but with missing data and I want to add them together with the NA's being ignored. Clearly I need to get the NA ignored. na.action? I have done some searching and cannot get na.action to help. This must be a common enough issue that the answer is staring me in the face but I just don't see it. Simple example a <- c(2, NA, 3) b <- c(3,4, 5) What I want is c <- a + b where c is ( 5 , 4 ,8) However I get c is (5,NA, 8) What am I missing? Or do I somehow need to recode the NA's as missing? Thanks
Rick Bilonick
2006-May-01 17:43 UTC
[R] Adding elements in an array where I have missing data.
On Mon, 2006-05-01 at 13:26 -0400, John Kane wrote:> This is a simple question but I cannot seem to find > the answer. > I have two vectors but with missing data and I want to > add them together with > the NA's being ignored. > > Clearly I need to get the NA ignored. na.action? > > I have done some searching and cannot get na.action to > help. > This must be a common enough issue that the answer is > staring me in the face > but I just don't see it. > > Simple example > a <- c(2, NA, 3) > b <- c(3,4, 5) > > What I want is > c <- a + b where > c is ( 5 , 4 ,8) > > However I get > c is (5,NA, 8) > > What am I missing? Or do I somehow need to recode the > NA's as missing? > > ThanksYour example shows that you are not ignoring the NA's but treating them as zeroes. This may or may not make sense depending on the application. Your comment "Or do I somehow need to recode the NA's as missing" is puzzling. NA denotes a missing value. If you insist on treating NA's as zeroes, then just replace the NA's with zeroes before adding:> a[is.na(a)] <- 0Rick B.> > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Assistant Professor University of Pittsburgh School of Medicine Department of Ophthalmology 412 648 9138 BST S 207
Dear John, The behaviour of R is reasonable, since, in your example, 4 + NA should in general be NA (i.e., missing). You can do what you want by changing NAs to 0s:> a[is.na(a)] <- 0 > a + b[1] 5 4 8 I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of John Kane > Sent: Monday, May 01, 2006 12:26 PM > To: R R-help > Subject: [R] Adding elements in an array where I have missing data. > > This is a simple question but I cannot seem to find the answer. > I have two vectors but with missing data and I want to add > them together with the NA's being ignored. > > Clearly I need to get the NA ignored. na.action? > > I have done some searching and cannot get na.action to help. > This must be a common enough issue that the answer is staring > me in the face but I just don't see it. > > Simple example > a <- c(2, NA, 3) > b <- c(3,4, 5) > > What I want is > c <- a + b where > c is ( 5 , 4 ,8) > > However I get > c is (5,NA, 8) > > What am I missing? Or do I somehow need to recode the NA's > as missing? > > Thanks > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
Gabor Grothendieck
2006-May-02 00:35 UTC
[R] Adding elements in an array where I have missing data.
Here are a few alternatives: replace(a, is.na(a), 0) + b ifelse(is.na(a), 0, a) + b mapply(sum, a, b, MoreArgs = list(na.rm = TRUE)) On 5/1/06, John Kane <jrkrideau at yahoo.ca> wrote:> This is a simple question but I cannot seem to find > the answer. > I have two vectors but with missing data and I want to > add them together with > the NA's being ignored. > > Clearly I need to get the NA ignored. na.action? > > I have done some searching and cannot get na.action to > help. > This must be a common enough issue that the answer is > staring me in the face > but I just don't see it. > > Simple example > a <- c(2, NA, 3) > b <- c(3,4, 5) > > What I want is > c <- a + b where > c is ( 5 , 4 ,8) > > However I get > c is (5,NA, 8) > > What am I missing? Or do I somehow need to recode the > NA's as missing? > > Thanks > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Berton Gunter
2006-May-02 15:19 UTC
[R] Adding elements in an array where I have missing data.
> > Here are a few alternatives: > > replace(a, is.na(a), 0) + b > > ifelse(is.na(a), 0, a) + b > > mapply(sum, a, b, MoreArgs = list(na.rm = TRUE)) >Well, Gabor, if you want to get fancy... evalq({a[is.na(a)]<-0;a})+b (and variants...) Cheers, Bert
Gabor Grothendieck
2006-May-02 17:41 UTC
[R] Adding elements in an array where I have missing data.
On 5/2/06, Berton Gunter <gunter.berton at gene.com> wrote:> > > > Here are a few alternatives: > > > > replace(a, is.na(a), 0) + b > > > > ifelse(is.na(a), 0, a) + b > > > > mapply(sum, a, b, MoreArgs = list(na.rm = TRUE)) > > > > Well, Gabor, if you want to get fancy... > > evalq({a[is.na(a)]<-0;a})+b >Note that the evalq can be omitted: { a[is.na] <- 0; a } + b