Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine two data frames with all posible combinations

I have 2 data frames.
How I can make something like tidyr::complete with them using tidyverse?

My data:

df <-data.frame(a=letters[1:2] )
df1<-data.frame(one=1:2)

Expected Result:

a 1 
b 1
a 2
b 2

Thx!

like image 751
jyjek Avatar asked Oct 14 '25 14:10

jyjek


1 Answers

You can use tidyr::crossing

tidyr::crossing(df, df1)

# A tibble: 4 x 2
#  a       one
#  <chr> <int>
#1 a         1
#2 a         2
#3 b         1
#4 b         2

or expand_grid

tidyr::expand_grid(df, df1)
like image 143
Ronak Shah Avatar answered Oct 17 '25 04:10

Ronak Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!