1

Suppose we have the contents of tables x and y in two dataframes in R. Which is the suggested way to perform an operation like the following in sql:

Select x.X1, x.X2, y.X3
into z
from x inner join y on x.X1 = y.X1

I tried the following in R. Is there a better way? Thank you

x<-data.frame(cbind('X1'=c(5,9,7,6,4,8,3,1,10,2),'X2'=c(5,9,7,6,4,8,3,1,10,2)^2))
y<-data.frame(cbind('X1'=c(9,5,8,2),'X3'=c('nine','five','eight','two')))

z<-cbind(x[which(x$X1 %in% (y$X1)), c(1:2)][order(x[which(x$X1 %in% (y$X1)), c(1:2)]$X1),],y[order(y$X1),2])
gd047
  • 29,749
  • 18
  • 107
  • 146

2 Answers2

4

This was already answered on stackoverflow.

Beyond merge, if you're more comfortable with SQL you should check out the sqldf package, which allows you to run SQL queries on data frames.

 library(sqldf)
 z <- sqldf("SELECT X1, X2, X3 FROM x JOIN y
      USING(X1)")

That said, you will be better off learning the base R functions (merge, intersect, union, etc.) in the long run.

Shane
  • 98,550
  • 35
  • 224
  • 217
1

Ok, it was easy merge(x,y)

gd047
  • 29,749
  • 18
  • 107
  • 146