[R] Handling "NA" in summation

Dan D ddalthorp at usgs.gov
Mon Sep 7 01:50:38 CEST 2015


# it's not clear what your question is, but here's a stab in the dark at a
solution!

ind<- !is.na(dataframe$A) & !is.na(dataframe$B)
dataframe$A[ind] + dataframe$B[ind] 

- Dan

P.S. I'm sure there are ways to do this using one of R's functions for
automatically removing NA's (na.rm = T), but unless you use them all the
time, their behavior is not always predictable (e.g., sometimes it ignores a
whole row with one NA in it; sometimes it treats NAs as zeros). Explicitly
defining  the indices that you want to exclude may make it easier to avoid
difficult-to-find errors.
 



--
View this message in context: http://r.789695.n4.nabble.com/Handling-NA-in-summation-tp4711923p4711932.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list