r - Cumprod ignoring NA -
this question has answer here:
i'd produce dataframe applied comprod function ignoring na
x = data.frame(a=c(na,1,2,3),b=c(na,5,6,7)) > cumprod(x) b 1 na na 2 na na 3 na na 4 na na
the result want is,
> cumprod(x) b 1 na na 2 1 5 3 2 30 4 6 210
any simple , efficient idea?
anything multiplied 1 again, so:
x[] <- lapply(x, function(i) cumprod(replace(i,is.na(i),1)) * ifelse(is.na(i),na,1) ) x # b #1 na na #2 1 5 #3 2 30 #4 6 210
Comments
Post a Comment