我想创建一个名为“Standard”的新列,作为三个测试结果的合成(我的数据集中有三个不同的列)。
我在数据集中有大约100条记录,我想在R中找到一种方法,而不必手动进行。我没有尝试任何东西,因为我不知道该尝试什么,我是R的新手。任何帮助或暗示都会非常感激。感谢。
数据集如下所示:
ID Test_1 Test_2 Test_3
1 positive positive positive
2 positive negative positive
3 positive positive negative
4 negative negative positive
5 negative positive negative
6 negative negative negative
7 positive negative negative
以下是我期望最终数据集的样子:
ID Test_1 Test_2 Test_3 Standard
1 positive positive positive Strong
2 positive negative positive Medium
3 positive positive negative Medium
4 negative negative positive Scanty
5 negative positive negative Scanty
6 negative negative negative Failed
7 positive negative negative Scanty
答案 0 :(得分:1)
这里有一个使用rowSums
索引标准值向量的选项(注意它们必须按照正确的顺序排列)
idx <- rowSums(df[-1] == "positive") +1L
df$Standard <- c("Failed", "Scanty", "Medium", "Strong")[idx]
结果:
> df
ID Test_1 Test_2 Test_3 Standard
1 1 positive positive positive Strong
2 2 positive negative positive Medium
3 3 positive positive negative Medium
4 4 negative negative positive Scanty
5 5 negative positive negative Scanty
6 6 negative negative negative Failed
7 7 positive negative negative Scanty
答案 1 :(得分:0)
ifelse
是一种方法
df$new <- apply(df[,-1], 1, function(i) length(i[i == 'positive']))
df$standard <- ifelse(df$new == 3, 'Strong', ifelse(df$new == 2, 'Medium', ifelse(df$new == 1, 'Scanty', 'Failed')))
df
# ID Test_1 Test_2 Test_3 new standard
# 1 1 positive positive positive 3 Strong
# 2 2 positive negative positive 2 Medium
# 3 3 positive positive negative 2 Medium
# 4 4 negative negative positive 1 Scanty
# 5 5 negative positive negative 1 Scanty
# 6 6 negative negative negative 0 Failed
# 7 7 positive negative negative 1 Scanty
答案 2 :(得分:0)
您还可以使用包mutate
中的plyr
。它可以通过添加新列或替换现有列来改变数据框。