我有一个目前看起来像这样的数据集:
Actor1 Actor2
1 2
1 4
2 5
1 3
2 6
4 5
2 7
3 7
我拥有的另一个数据集可以识别每个数字的含义。像这样:
ID Label
1 Walmart
2 Apple
3 Microsoft
4 Vodafone
5 BMW
6 Berkshire Hathaway
7 Bank of America
我需要使用第二个数据集来确定Actor在第一个数据集中是谁,并且我希望最终的数据集看起来像:
Actor1 Actor2
Walmart Apple
Walmart Vodafone
Apple BMW
.
.
.
等等。我最初使用ifelse函数并手动执行,但是花费的时间太长。然后,我通过创建三个数据文件(文件1:演员1;文件2:演员2;文件3:ID和名称)尝试了合并功能。但是,Actor1和Actor2列之间的顺序混乱了。
感觉这应该很简单,但是我很困惑。有什么好主意吗?
先感谢您。
基本R方法
d <- with(df2, setNames(ID, Label))
list2DF(Map(function(x, y) names(d)[match(x, y)], df1, list(d)))
给
Actor1 Actor2
1 Walmart Apple
2 Walmart Vodafone
3 Apple BMW
4 Walmart Microsoft
5 Apple Berkshire Hathaway
6 Vodafone BMW
7 Apple Bank of America
8 Microsoft Bank of America
一个短得多的(感谢@akrun的评论)
df1[] <- df2$Label[as.matrix(df1)]
数据
> dput(df1)
structure(list(Actor1 = c("Walmart", "Walmart", "Apple", "Walmart",
"Apple", "Vodafone", "Apple", "Microsoft"), Actor2 = c("Apple",
"Vodafone", "BMW", "Microsoft", "Berkshire Hathaway", "BMW",
"Bank of America", "Bank of America")), row.names = c(NA, -8L
), class = "data.frame")
> dput(df2)
structure(list(ID = 1:7, Label = c("Walmart", "Apple", "Microsoft",
"Vodafone", "BMW", "Berkshire Hathaway", "Bank of America")), class = "data.frame", row.names =
c(NA,
-7L))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句