-1

I am trying to combine two dataframes in an specific way and creat a new dataframe, and avoid for loops.
Assume a dataframe 1 like,

DF1  
    chan   
1    A01  
2    A02  
3    A03  
4    A04  

and dataframe 2 is:

DF2  
     Len  
1     10  
2     11  
3     12

I need to creat the third dataframe that look like (without using for loops):

DF3  
     chan  Len  
1     A01   10  
2     A01   11  
3     A01   12  
1     A02   10  
2     A02   11  
3     A02   12

Appreciate answers in R and/or python. Thanks a lot in advance!

Adrian
  • 1
  • 1
  • 1
    Possible duplicate of [Pandas Merging 101](https://stackoverflow.com/questions/53645882/pandas-merging-101) – pault Jun 20 '19 at 18:21
  • 1
    *Appreciate answers in R and/or python* ... SO is not a code-writing service. Please make an attempt and then ask a specific coding issue to troubleshoot. – Parfait Jun 20 '19 at 18:28

2 Answers2

0

Adrian, Try

df3 <- expand.grid(df1$chan, df2$Len)
greengrass62
  • 968
  • 7
  • 19
0

I think what you want to do is a cross join. Several packages can help you do this like base R's merge function, join functions from the dplyr package, etc. I like to use the crossing function from the tidyr package.

DF1 <- tibble(Chan = c("A01","A02","A03","A04"))
DF2 <- tibble(Len = 10:12)

tidyr::crossing(A, B)

# A tibble: 12 x 2
   A         B
   <chr> <int>
 1 A01      10
 2 A01      11
 3 A01      12
 4 A02      10
 5 A02      11
 6 A02      12
 7 A03      10
 8 A03      11
 9 A03      12
10 A04      10
11 A04      11
12 A04      12
Kay
  • 2,057
  • 3
  • 20
  • 29