【发布时间】:2020-06-21 06:01:02
【问题描述】:
目前我有四列需要合并/合并为一列。每行要么有四个“0”,要么有一个“1”和三个“0”。
这是数据框现在的样子:
id L1_Correct L2_Correct L3_Correct L4_Correct
1 0 0 0 1
2 1 0 0 0
3 0 1 0 0
4 1 0 0 0
5 0 0 1 0
这就是我希望数据框的样子:
id L1_Correct L2_Correct L3_Correct L4_Correct Combined__L_Accuracy
1 0 0 0 1 1
2 1 0 0 0 1
3 0 0 0 0 0
4 1 0 0 0 1
5 0 0 0 0 0
我尝试在不同的场合使用粘贴功能和联合功能:即,
L_Data$Combine_L_Accuracy <- paste(L_Data$L1_Correct, L_Data$L2_Correct, L_Data$L3_Correct, L_Data$L4_Correct)
L_Data_all <- L_Data %>% unite(Combine_L_Accuracy, L1_Correct, L2_Correct, L3_Correct, L4_Correct, na.rm = TRUE, remove = TRUE)
L_Data_all
但他们都给了我这样的结果:0_0_0_0 根据四列中的值,我只需要最后一列中的 0 或 1 值。
【问题讨论】:
-
这是你想要的吗?
dataframe$Combined__L_Accuracy <- rowSums(dataframe) -
我在数据框中还有一堆其他列,有没有办法只指定我想要的四列?