【发布时间】:2012-06-18 03:35:21
【问题描述】:
可能重复:
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")
我的问题是:
这是否意味着merge在此处不执行内连接。
如何在基础 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))
【问题讨论】:
-
您对
merge的使用与您链接到的问题中如何进行内部连接的描述不符。 -
sqldf 输出与链接中的示例相匹配。有没有办法通过合并来做到这一点?我在几个地方读过合并是一个内部连接,所以我也试图更好地理解这个定义。谢谢。
-
正如我所说,将您的代码
merge(employee,department,all = T)与您在上面链接到的问题中进行内部连接的说明进行比较。您没有按照那里接受的答案指示您这样做。