0

I have 3 data frames

d1=data.frame(Modelo = sample(c("ModeloA","ModeloB","ModeloC","ModeloD"), 20, replace=TRUE),
              Color = sample(c("ColorA","ColorB","ColorC"), 20, replace=TRUE),
              Id=1:20)

d2=data.frame(Modelo = sample(c("ModeloD","ModeloB","ModeloE","ModeloD"), 30, replace=TRUE),
              Color = sample(c("ColorA","ColorF","ColorC"), 30, replace=TRUE),
              Id=1:30)

d3=data.frame(Modelo = sample(c("ModeloA","ModeloB","ModeloD"), 20, replace=TRUE),
              Color = sample(c("ColorA","ColorA","ColorC","ColorD"), 20, replace=TRUE),
              Id=1:20)

I unify them in a single data frame

col<-c("Modelo", "Color")

d1 %>% inner_join(d2,by=col, suffix=c(".d1", ".d2"))-> d1_2

d1_2 %>% inner_join(d3,by=col) -> d12_3

d12_3 <-rename(d12_3, c("Id.d3"="Id"))

I want to summarize the d12_3 as follows

Modelo    Color     Id.d1           Id.d2        Id.d3
ModeloB  ColorA     18              10           9,16 ,17,19
ModeloB  ColorC     3,15,20         7,8,17,22    11
ModeloD  ColorA     12              11,24,28     1,6,12,13

it's possible?

DudaR
  • 127
  • 5

1 Answers1

1

As the comments said if you generate a random dataset its best to add a set.seed otherwise it is difficult to generate the same result.

It is also not clear what you want to archive in your summarize to me, I think it's unique values? Following code groups by cols Modelo and Color and extract the unique values into a list or alternative into a string.

# List
result <- d12_3 %>% 
  group_by(Modelo, Color) %>% 
  summarise(
    Id.d1 = list(unique(Id.d1)),
    Id.d2 = list(unique(Id.d2)),
    Id.d3 = list(unique(Id.d3))
    )
# String
result <- d12_3 %>% 
  group_by(Modelo, Color) %>% 
  summarise(
    Id.d1 = str_c(unique(Id.d1), sep = ","),
    Id.d2 = str_c(unique(Id.d2), sep = ","),
    Id.d3 = str_c(unique(Id.d3), sep = ",")
  )

Cheers Hannes