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!
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)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With