Home > database >  replace one value with other value in every units of panel data
replace one value with other value in every units of panel data

Time:03-17

i have a panel data. the variable of interest have two values,1 and 0. i would like to replace 0 with 1 by group if 1 is in front of 0 . The data is as follow,

id <- c(1,1,2,2,2,3,3,3,4,4);
burden <- c(0,0,0,1,0,1,0,0,0,1)

i expect to get the following results,

burden <- c(0,0,0,1,1,1,1,1,0,1)

Thanks for any help in advance!

CodePudding user response:

This is my solution using base R

# Splitting Burden into groups according to the value in id
split_burden <- split(burden,id)
#$`1`
#[1] 0 0
#
#$`2`
#[1] 0 1 0
#
#$`3`
#[1] 1 0 0
#
#$`4`
#[1] 0 1

# for all groups apply cummax to each group with sapply
# if there is a 1 the value of cummax for all following
# elements in this group will be 1
split_burden_filled <- sapply(split_burden, cummax)
#$`1`
#[1] 0 0
#
#$`2`
#[1] 0 1 1
#
#$`3`
#[1] 1 1 1
#
#$`4`
#[1] 0 1

# Put the groups back together to one vector
solution <- unsplit(split_burden_filled,id)
#[1] 0 0 0 1 1 1 1 1 0 1

Note that this is only always correct if burden is binary.

CodePudding user response:

You can just use tapply to look across groups.

id <- c(1,1,2,2,2,3,3,3,4,4)
burden <- c(0,0,0,1,0,1,0,0,0,1)

unlist(tapply(burden, id, \(x) cummax(x)))
#> 11 12 21 22 23 31 32 33 41 42 
#>  0  0  0  1  1  1  1  1  0  1
  • Related