cumsum along row of data.frame with NA in R

后端 未结 2 470
孤独总比滥情好
孤独总比滥情好 2021-01-23 05:54

The hypothetical case is that there exist NA in a data.frame

> a <- c(1:5, NA, 7:10)
> b <- 1:10
> c <- 1:10
> 
> data <-         


        
2条回答
  •  梦毁少年i
    2021-01-23 06:22

    Given your desired result (where you don't mind NA becoming 0), I guess the easiest thing is to first remove the NA values using is.na and then carry on as before.

    data[ is.na(data) ] <- 0
    data.frame(t(apply(data,1,cumsum)))
    

提交回复
热议问题