Search code examples
rdplyrindicator

assigning a value corresponding to an indicator in r


id time value1 indicator value2
1 2004 1 1 1
1 2005 0 0 1
1 2006 -1 0 1
1 2007 -3 0 1
1 2008 3 0 1
2 2004 1 0 NA
2 2005 3 0 NA
2 2006 0 0 NA
2 2007 0 0 NA
3 2004 -1 1 -1
3 2005 -3 0 -1
3 2006 4 0 -1
3 2007 5 0 -1
4 2004 4 0 -4
4 2005 5 0 -4
4 2006 2 0 -4
4 2007 -4 1 -4

Indicator is a binary variable. The desired output is value 2. If indicator is 0, its value 2 takes a value 2 of the year when indicator is 1. It is grouped by id. If an id does not have any 1 for indicator, value2 takes NA's.


Solution

  • library(dplyr)
    df %>%
      group_by(id) %>% 
      mutate(value2 = ifelse(indicator == 1, value1 , NA)
      ) %>% 
      tidyr::fill(value2, .direction  = "updown")
    

    Result:

          id  time value1 indicator value2
       <int> <int>  <int>     <int>  <int>
     1     1  2004      1         1      1
     2     1  2005      0         0      1
     3     1  2006     -1         0      1
     4     1  2007     -3         0      1
     5     1  2008      3         0      1
     6     2  2004      1         0     NA
     7     2  2005      3         0     NA
     8     2  2006      0         0     NA
     9     2  2007      0         0     NA
    10     3  2004     -1         1     -1
    11     3  2005     -3         0     -1
    12     3  2006      4         0     -1
    13     3  2007      5         0     -1
    14     4  2004      4         0     -4
    15     4  2005      5         0     -4
    16     4  2006      2         0     -4
    17     4  2007     -4         1     -4