Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment by group

Tags:

r

dplyr

I'm trying to increment a column per group. So if there is a value then we increment it based on the value before it, otherwise we leave it.

So for example it would go from df to dfb.

df <- data.frame(group = c("A", "A", "B", "B", "B", "C", "C", "C", "D", "D"), 
                 num = c(1, NA, NA, 8, NA, 5, NA, NA, 10, NA))
dfb <- data.frame(group = c("A", "A", "B", "B", "B", "C", "C", "C", "D", "D"),
                 num = c(1, 2, NA, 8, 9, 5, 6, 7, 10, 11))
> df

   group num
1      A   1
2      A  NA
3      B  NA
4      B   8
5      B  NA
6      C   5
7      C  NA
8      C  NA
9      D  10
10     D  NA

> dfb
   group num
1      A   1
2      A   2
3      B  NA
4      B   8
5      B   9
6      C   5
7      C   6
8      C   7
9      D  10
10     D  11

My best attempt was this but it did not work

dfc <- df %>%
   mutate(num = ifelse(is.na(num),lag(num) + 1, num))

Deleted my previous question because my problem previously badly defined. Thanks for the help!

like image 665
tonyk Avatar asked Feb 24 '17 11:02

tonyk


1 Answers

We can do

df %>% 
 group_by(grp1= cumsum(!is.na(num)), group) %>%
 mutate(num = if(n() > 1) num[1L] + row_number()-1 else num) %>% 
 ungroup() %>%
 select(-grp1)
# A tibble: 10 × 2
#    group   num
#   <fctr> <dbl>
#1       A     1
#2       A     2
#3       B    NA
#4       B     8
#5       B     9
#6       C     5
#7       C     6
#8       C     7
#9       D    10
#10      D    11

Or with data.table

library(data.table)
setDT(df)[, num := if(.N >1) num[1L] + seq_len(.N)-1
            else num,.(grp1=cumsum(!is.na(num)), group)]
like image 152
akrun Avatar answered Nov 03 '22 01:11

akrun