Possible Duplicate:
How to join data frames in R (inner, outer, left, right)?
This question nicely goes over joins in R.
And the Wikipedia article on inner join is useful.
I would like to reproduce this result in base R. I don't think the following work:
merge(employee,department,all=T)
merge(employee,department)
because of the duplicates on the merging variable.
However, sqldf
works:
library(sqldf)
sqldf("select * from employee inner join department ON employee.DepartmentID = department.DepartmentID")
My questions are:
Does this mean that merge does not perform an inner join here.
How can I accomplish this join in base R.
Here are two data.frames to work with from the website and example above.
employee <- structure(list(LastName = c("Rafferty ", "Jones", "Steinberg",
"Robinson", "Smith", "John"), DepartmentID = c("31", "33", "33",
"34", " 34", " .")), .Names = c("LastName", "DepartmentID"), class = "data.frame", row.names = c(NA, -6L))
department <- structure(list(DepartmentID = c(31L, 33L, 34L, 35L), DepartmentName = structure(c(4L,2L, 1L, 3L), .Label = c("Clerical", "Engineering", "Marketing", "Sales"), class = "factor")), .Names = c("DepartmentID", "DepartmentName"), class = "data.frame", row.names = c(NA, -4L))