Search code examples
rdplyrrowsum

Conditional value change across columns


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)

Solution

  • 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