可能重复:
How to join data frames in R (inner, outer, left, right)?
This question很好地介绍了R中的联接。
内连接上的Wikipedia article非常有用。
我想在基础R中重现这个结果。我不认为以下工作:
merge(employee,department,all=T)
merge(employee,department)
因为合并变量有重复。
但是,sqldf
有效:
library(sqldf)
sqldf("select * from employee inner join department ON employee.DepartmentID = department.DepartmentID")
我的问题是:
这是否意味着合并不会在此处执行内部联接。
如何在基础R中完成此连接。
以下是两个可以从网站和上面的例子中使用的数据框架。
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))
答案 0 :(得分:3)
Julieth,
请查看这是否适合您。我检查了数据结构的结构,看看为什么结果不符合预期。
str(department)
str(employee)
employee$DepartmentID <- as.numeric(employee$DepartmentID)
merge(employee,department)