tags:

views:

159

answers:

2

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])
+1  A: 

Ok, it was easy merge(x,y)

gd047
+4  A: 

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
interesting, I did know of RMySQL, but not of sqldf.. looks cool.
dalloliogm