简体   繁体   English

根据其他列中值的变化添加新列

[英]Add a new column based on change in values in other columns

I have the following dataframe:我有以下 dataframe:

DF <- data.frame(Col1=c(0,0,1),Col2=c(0,1,1),Col3=c(1,0,1))
Col1列1 Col2列2 Col3列3
1 1个 0 0 0 0 1 1个
2 2个 0 0 1 1个 0 0
3 3个 1 1个 1 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:我需要添加一个新列“Switch”,其中包含行值首次更改时的变量名称,因此 output 如下所示:

Col1列1 Col2列2 Col3列3 Switch转变
1 1个 0 0 0 0 1 1个 Col3列3
2 2个 0 0 1 1个 0 0 Col2列2
3 3个 1 1个 1 1个 1 1个 NA北美

Any guidance or help will be appreciated.任何指导或帮助将不胜感激。 Thank you.谢谢你。

We may use max.col我们可以使用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.您可以编写一个带有diff的 function 并按行应用它。

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 -您也可以使用dplyr -

library(dplyr)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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