R中的聚合和重构两个表

问题描述 投票:0回答:1

我有两张这样的桌子;

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")

我没有得到我想要的结果

r reshape
1个回答
2
投票

您可以先在

melt
上使用
table2
,然后用
left_join
执行
table1

这可以通过

dplyr
tidyr
来实现。

library(dplyr)
library(tidyr)
table2_long <- table2 |> 
  pivot_longer(-Test.Name,
               names_to="Comp.Name")

table3 <- inner_join(table1, table2_long, by= c("Test"="Test.Name", "Comp.Name"))

输出

    Test Comp.Name value
1 A Test    Comp.1     5
2 B Test    Comp.1    10
3 B Test    Comp.2    15
4 C Test    Comp.2     8
5 A Test    Comp.3     7
6 B Test    Comp.4    25

也可以通过

reshape

实现
library(reshape2)
table2_long <- table2 |> 
                 melt(id.cols = "Test.Name",
                      variable.name = "Comp.Name")
table3 <- merge(table1, table2_long, by.x = c("Test","Comp.Name"), by.y = c("Test.Name","Comp.Name"))
© www.soinside.com 2019 - 2024. All rights reserved.