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. 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@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.