0

I wanted to use cummean by groups ignoring NA. Here is the sample data.

data.frame(group1 = c(rep(1,3),rep(2,3),rep(3,3)),
           val = c(c(NA,4,5),c(NA,NA,5),c(1,NA,3)))

How can I get cumulative mean by group ignoring NA?

Here is my desired out

c(NA,4,4.5,NA,NA,5,1,1,2)
John legend2
  • 840
  • 9
  • 18
  • 1
    I don't think the duplicate link address the question OP asked – akrun Oct 24 '20 at 20:00
  • Here is more appropriate duplicate: https://stackoverflow.com/questions/49906503/using-cummean-with-group-by-and-ignoring-nas – missuse Oct 24 '20 at 20:02

1 Answers1

2

We can use replace to ignore the NA elements in calculating the cummean

library(dplyr)
library(tidyr)
df1 %>%
     group_by(group1) %>%
     mutate(new = replace(val, !is.na(val), cummean(val[!is.na(val)]))) %>%
     fill(new)

-output

# A tibble: 9 x 3
# Groups:   group1 [3]
#  group1   val   new
#   <dbl> <dbl> <dbl>
#1      1    NA  NA  
#2      1     4   4  
#3      1     5   4.5
#4      2    NA  NA  
#5      2    NA  NA  
#6      2     5   5  
#7      3     1   1  
#8      3    NA   1  
#9      3     3   2  
 
akrun
  • 874,273
  • 37
  • 540
  • 662