Search code examples
rreshape

Aggregation and Restructuring two table in R


I have two tables like that;

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

i want to get the following table:

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

I will try inner_join and reshape functions in R, The best I've been able to do so far is something like:

:

table3 <- inner_join(table1, table2, by.x = "Test", by.y="Test Name") %>%
  select("Test Name", "Comp Name", "Test Price" = "Comp Name")

I couldn't get the result I wanted.


Solution

  • You can first use melt on table2 and then perform a left_join with table1

    This can be achieved with dplyr and 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"))
    
    

    Output

        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
    

    It can be achieved as well with 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"))