简体   繁体   中英

Add a new column based on change in values in other columns

I have the following dataframe:

DF <- data.frame(Col1=c(0,0,1),Col2=c(0,1,1),Col3=c(1,0,1))
Col1 Col2 Col3
1 0 0 1
2 0 1 0
3 1 1 1

I need to add a new column "Switch" that contains the name of the variable at which the value of the row has changed for the first time, so the output looks like this:

Col1 Col2 Col3 Switch
1 0 0 1 Col3
2 0 1 0 Col2
3 1 1 1 NA

Any guidance or help will be appreciated. Thank you.

We may use max.col

tmp <- names(DF)[max.col(DF, 'first')]
tmp[rowSums(DF == 1) == ncol(DF)|rowSums(DF == 0) == ncol(DF)] <- NA
DF$Switch <- tmp

-output

> DF
  Col1 Col2 Col3 Switch
1    0    0    1   Col3
2    0    1    0   Col2
3    1    1    1   <NA>

You may write a function with diff and apply it rowwise.

switch_col <- function(x) {
  cols[which(diff(x) != 0)[1] + 1]
}
cols <- names(DF)
DF$switch_col <- apply(DF, 1, switch_col)
DF

#  Col1 Col2 Col3 switch_col
#1    0    0    1       Col3
#2    0    1    0       Col2
#3    1    1    1       <NA>

You may also use dplyr -

library(dplyr)

DF %>%
  rowwise() %>%
  mutate(switch_col = switch_col(c_across())) %>%
  ungroup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM