我有两个数据框。第一个是工作:
Jobs <- data.frame(Company = c("A","B"), Name = c("Peter","Peter"), Job = c("CEO","Member of the Board"))
第二个是Media_Appearence:
Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"))
有没有办法,使用基本的R命令,在Media_Appearence上插入一个新列,使用匹配的参数填充它(检查前两列并返回匹配的作业)?所需的输出是:
Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"),Job= c("CEO","CEO","Member of the Board","Member of the Board","CEO","CEO","CEO","CEO"))
已经尝试合并,但结果很混乱。
由于
答案 0 :(得分:3)
您想要做的通常称为“加入”,“合并”或“追加”数据。如果你不能这样做,R就不会是一种语言!
匹配的字段称为“密钥”。在这种情况下,R假设您的密钥为Company
,因为它是他们共同的唯一字段。
Jobs <- data.frame(Company = c("A","B"), Name = c("Peter","Peter"), Job = c("CEO","Member of the Board"))
Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"))
merge(Media_Appearence, Jobs)
Company Name Job 1 A Peter CEO 2 A Peter CEO 3 A Peter CEO 4 A Peter CEO 5 A Peter CEO 6 A Peter CEO 7 B Peter Member of the Board 8 B Peter Member of the Board
您可能已经注意到您的数据已经为您排序了。它按默认键排序,在本例中为Company
。
答案 1 :(得分:1)
通过使用匹配,不会更改data.frame的原始顺序
Media_Appearence$Job=Jobs$Job[match(Media_Appearence$Company, Jobs$Company)]
Media_Appearence
Company Name Job
1 A Peter CEO
2 A Peter CEO
3 B Peter Member of the Board
4 B Peter Member of the Board
5 A Peter CEO
6 A Peter CEO
7 A Peter CEO
8 A Peter CEO