0

I am looking to (I think) melt two dataframes together in R (or python but I am working in R at the moment). I might have been looking in all the wrong corners by not knowing the correct wording for my problem, but nevertheless I cannot find an answer.

I have two dataframes (datA, datB), which are of differing sizes and I need to combine together to create one 'long' dataframe - datC.

datA <- data.frame("Val1" = c(1, 2, 3),
              "Val2" = c(0,1,1))


datB <- data.frame("Val3" = c("A","B","C","D"),
              "Val4" = c(15,25,35,45))

I am looking to combine these dataframes into datC, I don't mind if datC is created from datA or datB as these dataframes will be removed once I have my final dataset anyway.

datC should look like:

datC <- data.frame("Val1" = c(1,1,1,1,2,2,2,2,3,3,3,3),
              "Val2" = c("A","B","C","D","A","B","C","D","A","B","C","D"),
              "Val3" = c(0,0,0,0,1,1,1,1,1,1,1,1),
              "Val4" = c(15,25,35,45,15,25,35,45,15,25,35,45))
Val1       Val2       Val3       Val4
1          A          0          15
1          B          0          25
1          C          0          35
1          D          0          45
2          A          1          15
2          B          1          25
2          C          1          35
2          D          1          45
3          A          1          15
3          B          1          25
3          C          1          35
3          D          1          45

As I said, I am not sure what to actually call my problem, so looking for solutions has been a challenge. Once I am aware of what I am looking for I will change the title to help others out.

EDIT: The answer below works merge(datA, datB, all=TRUE),

This question has already been asked, see this post : combine two data frames with all posible combinations

I don't know if this means this post has been closed or not, but if it hasn't I won't delete this as it might help others who, like me, don't know what to search for.

Thank you very much for the speedy help

camille
  • 16,432
  • 18
  • 38
  • 60

1 Answers1

0

It's called a cross join (or cartesian product) and can do it easy enough in base R

merge(datA,datB,all=TRUE)
Quixotic22
  • 2,894
  • 1
  • 6
  • 14