[R] how to ignore NA when using cumsum WHILE retaining NAs?

William Dunlap wdunlap at tibco.com
Sat Nov 21 21:35:26 CET 2009


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Tracy Bowerman
> Sent: Saturday, November 21, 2009 12:19 PM
> To: r-help
> Subject: [R] how to ignore NA when using cumsum WHILE retaining NAs?
> 
> I would like to cumulatively sum rows in a matrix, in which 
> each row has 1
> NA value, which I do NOT want to treat as 0s.  The NAs are 
> placeholders
> where there is actually no data, which is not the same as a 
> 0.  The usual
> "na.rm=TRUE" does not seem to work with the command cumsum.  Is there
> another way to ignore the NAs or do I need to figure out a 
> different way to
> do this?
> 
> Here's an example matrix of title "proportion":
> 
>         Ntrail    Strail      NFJD     Baldy     Onion      Crane
> [1,]        NA 0.2944937 0.1779969 0.1808015 0.2296511 0.11705683
> [2,] 0.2882713        NA 0.1795668 0.1823961 0.2316766 0.11808925
> [3,] 0.1716890 0.1769419        NA 0.3518116 0.2025204 0.09703714
> [4,] 0.1726311 0.1779128 0.3482548        NA 0.2036317 0.09756961
> [5,] 0.2252904 0.2321833 0.2059743 0.2092197        NA 0.12733223
> [6,] 0.2052614 0.2115415 0.1764081 0.1791877 0.2276013         NA
> 
> I want cumulative sums for each row but maintain NAs.  I do 
> NOT want to
> treat NAs as 0s, otherwise the cumulative sums add 0 and 
> returns a number
> where a NA should be.

It would really help if you supplied the expected
output for your example.  Does the following do
what you want?
   f <- function(mat) {
      # use t() if you want output to be organized in rows like input
      retval <- t(apply(replace(mat,is.na(mat),0), 1, cumsum))
      replace(retval, is.na(mat), NA)
   }
A slightly different approach that does the same thing is
   g <- function(mat) {
      for(i in seq_len(nrow(mat))) {
         isNotNA <- !is.na(mat[i,])
         mat[i,isNotNA] <- cumsum(mat[i,isNotNA])
      }
      mat
   }
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> Here's the code that works until it reaches an NA, then returns all
> subsequent values as NA.  Adding na.rm=TRUE returns the error:
> Error in FUN(newX[, i], ...) :  2 arguments passed to 'cumsum' which
> requires 1
> 
> cumsums <- apply(proportion, 1, cumsum)
> 
> Thank you for any suggestions,
> Tracy
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
> 




More information about the R-help mailing list