我有两张这样的table;
Test Comp.Name
A Test Comp.1
B Test Comp.1
B Test Comp.2
C Test Comp.2
A Test Comp.3
B Test Comp.4
table1 <- data.frame("Test" = c("A Test", "B Test", "B Test", "C Test", "A Test", "B Test"),
"Comp Name" = c("Comp.1", "Comp.1", "Comp.2", "Comp.2", "Comp.3", "Comp.4"))
Test.Name Comp.1 Comp.2 Comp.3 Comp.4
A Test 5 6 7 8
B Test 10 15 20 25
C Test 2 8 3 212
table2 <- data.frame("Test Name" = c("A Test", "B Test", "C Test"),
"Comp.1" = c(5, 10, 2),
"Comp.2" = c(6, 15, 8),
"Comp.3" = c(7, 20, 3),
"Comp.4" = c(8, 25, 212))
我想得到下表:
Test Comp.Name Test.Price
A Test Comp.1 5
B Test Comp.1 10
B Test Comp.2 15
C Test Comp.2 8
A Test Comp.3 7
B Test Comp.4 25
table3 <- data.frame("Test" = c("A Test", "B Test", "B Test", "C Test", "A Test", "B Test"),
"Comp Name" = c("Comp.1", "Comp.1", "Comp.2", "Comp.2", "Comp.3", "Comp.4"),
"Test Price" = c(5,10,15,8,7,25))
我将尝试在R中使用inner_join和reshape函数,到目前为止我能做的最好的是这样的:
:
table3 <- inner_join(table1, table2, by.x = "Test", by.y="Test Name") %>%
select("Test Name", "Comp Name", "Test Price" = "Comp Name")
我没能得到我想要的结果。
1条答案
按热度按时间6yjfywim1#
您可以先在
table2
上使用melt
,然后在table1
上执行left_join
这可以通过
dplyr
和tidyr
来实现。输出
它也可以通过
reshape
来实现