我想向 a 添加一个新列,该列将第一列中的数值data.frame
转换为后续匹配列中的相应字符串(如果有),即列名与第一列中的该值部分匹配。
在此示例中,我希望为“Highest_Earner”添加一个值,该值取决于Earner_Number
列中的值:
> df1 <- data.frame("Earner_Number" = c(1, 2, 1, 5),
"Earner5" = c("Max", "Alex", "Ben", "Mark"),
"Earner1" = c("John", "Dora", "Micelle", "Josh"))
> df1
Earner_Number Earner5 Earner1
1 1 Max John
2 2 Alex Dora
3 1 Ben Micelle
4 5 Mark Josh
结果应该是:
> df1
Earner_Number Earner5 Earner1 Highest_Earner
1 1 Max John John
2 2 Alex Dora Neither
3 1 Ben Micelle Michelle
4 5 Mark Josh Mark
我试过把它data.frame
切成各种小块,但想知道是否有人有更清洁的方法?