2

I need to calculate when a value switched between 0 and 1, values are distributed across columns, the switch is not given, and NAs are present.

I attempted with mutate and rowSums with little results.

Example:

df <- data.frame(entry = c(1:5), 
                year_1 = c(NA, NA, NA, 1, NA),
                year_2 = c(NA, NA, 0, 0, 1),
                year_3 = c(NA, 1, 1, 0, 1))

Desired result:

switch = c(NA, NA, "year_2", NA, NA)
MCS
  • 1,071
  • 9
  • 23

2 Answers2

1
l <- apply(df[, -1], 1, function(x) 
        names(df)[1 + which(tail(x, -1) == 1 & head(x, -1) == 0)])
unlist(ifelse(lengths(l), l, NA))

# [1] NA       NA       "year_2" NA       NA  
IceCreamToucan
  • 28,083
  • 2
  • 22
  • 38
0

To calculate changes across columns, you can take the difference between 'lead' and 'lag' versions (column-wise) of the data. Get indices for differences of 1, and use these to create the 'switch':

ix <- which(df[ , 3:ncol(df)] - df[ , 2:(ncol(df) - 1)] == 1, arr.ind = TRUE) 
df$switch <- NA
df$switch[ix[ , 1]] <- paste0("year_", ix[ , 2])

df
#   entry year_1 year_2 year_3 switch
# 1     1     NA     NA     NA   <NA>
# 2     2     NA     NA      1   <NA>
# 3     3     NA      0      1 year_2
# 4     4      1      0      0   <NA>
# 5     5     NA      1      1   <NA>
Henrik
  • 65,555
  • 14
  • 143
  • 159