2

I have 2 single column datasets but dataset A has fewer rows than dataset B.

I am trying to create a second data frame with 2 columns combining the 2 datasets. Something like this:

A <- data.frame(A=c('x','y','z'))
B <- data.frame(B=c('a','b','c','d','e'))

I need to combine both to create the dataframe bellow,(repeating all elements of A until the next...):

C <- data.frame(C=c('x','x','x','x','x','y','y','y','y','y','z','z','z','z','z'),D=c('a','b','c','d','e'))

The original datasets are much larger than this so I am trying to figure out an easy way to do it.

M--
  • 25,431
  • 8
  • 61
  • 93

2 Answers2

2

An option is crossing from tidyr

library(tidyr)
crossing(A, B)
akrun
  • 874,273
  • 37
  • 540
  • 662
2

In base we can use exapnd.grid:

expand.grid(A$A,B$B)
M--
  • 25,431
  • 8
  • 61
  • 93