0

I have two dataframes ( 'Correct answer' and 'Value' ). The Correct Answer has rows 1,2,3...10 and the Values has rows (1,2,3).

I essentially want to create a new dataframe with values ( [1,1] [1,2], [1,3], [2,1], [2,2] ...[Correctanswer(i), Value(j)].

Is there any way to do this without using a for loop?

www
  • 38,575
  • 12
  • 48
  • 84
  • please share what the structure of your data.frames look like using output of `dput(Correctanswer)` and `dput(Value)` in the question description. – tushaR Feb 03 '18 at 04:45
  • Are they vectors or data frames? If they are just vectors then this is a duplicate of [Unique combination of vectors](https://stackoverflow.com/q/11388359/903061) - use `expand.grid(v1, v2)`. If they are data frames with multiple columns then this is a duplicate of [How to do a cross join in R](https://stackoverflow.com/q/10600060/903061) - `merge(df1, df2, all = T)` – Gregor Thomas Feb 03 '18 at 04:52

1 Answers1

0

Try this:

df1=data.frame(a=1:10)
df2=data.frame(b=c(1,2,3))
as.data.frame(cbind(rep(df1$a,each=3),df2$b))
tushaR
  • 3,083
  • 1
  • 20
  • 33