Home > database >  How to fill NA with values by group and conditioning
How to fill NA with values by group and conditioning

Time:11-19

I'm struggling to figure out how I can repeat the value for id2 when it has value = NA, and also have the same date1.

I have the following data frame:

test = data.frame(date1 = c("2020-11-02", "2020-11-02","2020-11-02", "2020-11-03","2020-11-04","2020-11-04"),
                  value = c(676, 676, NA, 400, 505, NA),
                  id1 = c("16-4-L", "16-3-L", "16-3-R", "16-3-F", "17-6-L", "17-1-L"),
                  id2 = c("16", "16", "16", "16", "17", "17"))

#        date1 value    id1 id2
# 1 2020-11-02   676 16-4-L  16
# 2 2020-11-02   676 16-3-L  16
# 3 2020-11-02    NA 16-3-R  16
# 4 2020-11-03   400 16-3-F  16
# 5 2020-11-04   505 17-6-L  17
# 6 2020-11-04    NA 17-1-L  17

#and I need the following:
#> result
#        date1 value    id1 id2
# 1 2020-11-02   676 16-4-L  16
# 2 2020-11-02   676 16-3-L  16
# 3 2020-11-02   676 16-3-R  16
# 4 2020-11-03   400 16-3-F  16
# 5 2020-11-04   505 17-6-L  17
# 6 2020-11-04   505 17-1-L  17

Note that id2 are different, but I'm looking to the same id1 at the same date to use the same value.

I'd like to make something like: if value is an NA, then we look to rows that have the same id2 and date1, then just copy that value.

Any hint on how can I do that?

CodePudding user response:

Are you looking for sth. like:

library(tidyverse)
test %>%
  group_by(date1, id2) %>%
  fill(value, .direction = 'updown') %>%
  ungroup()

which gives:

# A tibble: 6 x 4
  date1      value id1    id2  
  <chr>      <dbl> <chr>  <chr>
1 2020-11-02   676 16-4-L 16   
2 2020-11-02   676 16-3-L 16   
3 2020-11-02   676 16-3-R 16   
4 2020-11-03   400 16-3-F 16   
5 2020-11-04   505 17-6-L 17   
6 2020-11-04   505 17-1-L 17
  • Related