[英]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![]() |
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:我需要添加一个新列“Switch”,其中包含行值首次更改时的变量名称,因此 output 如下所示:
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
我们可以使用
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.