基于列和命名向量创建新列

时间:2016-05-10 19:21:46

标签: r

我正在寻找一种快速简便的方法,通过从现有列和命名向量进行映射来在我的data.frame中创建新列。例如:

var iString = "int i; //A variable \n" + "//This is a text with notes \n" + "//Can you remove them? \n" + "cout<<i; //printing i \n" var notes1= ("/"+"(\\s)*"+"/"+"(\\w|[^\\w])*?"+"\\n").toRegex() println(iString.replace(notes1, ""))

从此我想要:

int i; cout<<i;

我希望能有这么简单的事情:

df <- data.frame(letters = LETTERS[1:8]) vec <- c("A" = "1", "B" = "2", "C" = "3", "D" = "1", "E" = "2", "F" = "3", "G" = "1", "H" = "2")

但是这给了我

letters numbers 1 A 1 2 B 2 3 C 3 4 D 1 5 E 2 6 F 3 7 G 1 8 H 2

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

感谢@Pierre Lafortune提供以下答案:

df$numbers <- vec[df$letters]