Home > OS >  Replacing Grouped Data in R
Replacing Grouped Data in R

Time:10-09

I want to create a new column based on conditions

My data frame looks like:

Id   Year  Code   Y
1    2009   0     0
1    2010   NA    NA
2    2009   0     0
2    2010   NA    NA
3    2009   1     1
3    2010   NA    NA
4    2009   2     1
4    2010   NA    NA

I need to replace values in my original Y variable in a way that returns 1 when the first year code for each individual is equal to 0 and the second year/code line is NA. The output that I'm looking for is:

Id   Year  Code    Y
1    2009   0      0
1    2010   NA     1
2    2009   0      0
2    2010   NA     1
3    2009   1      1
3    2010   NA     0
4    2009   2      1
4    2010   NA     0

Thanks in advance!

CodePudding user response:

This replace 0 to 1 and 1 to 0 if NA is present in Y column for each Id.

library(dplyr)

df %>%
  arrange(Id, Year) %>%
  group_by(Id) %>%
  mutate(Y = ifelse(is.na(Y), as.integer(!as.logical(na.omit(Y))), Y))

#     Id  Year  Code     Y
#  <int> <int> <int> <int>
#1     1  2009     0     0
#2     1  2010    NA     1
#3     2  2009     0     0
#4     2  2010    NA     1
#5     3  2009     1     1
#6     3  2010    NA     0
#7     4  2009     2     1
#8     4  2010    NA     0
  •  Tags:  
  • r
  • Related