2

可能重复:
如何在 R 中加入数据框(内、外、左、右)?

这个问题很好地解决了 R 中的连接问题。

并且关于内部连接的维基百科文章很有用。

我想在基础 R 中重现此结果。我不认为以下工作:

merge(employee,department,all=T)
merge(employee,department)

因为合并变量上的重复。

但是,sqldf有效:

library(sqldf)
sqldf("select * from employee  inner join department ON employee.DepartmentID = department.DepartmentID")

我的问题是:

  1. 这是否意味着 merge 不会在这里执行内部联接。

  2. 我怎样才能在base 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))
4

1 回答 1

3

朱丽叶,

请看看这是否适合你。我检查了数据结构的结构,看看为什么结果不是预期的。

str(department)
str(employee)
employee$DepartmentID <- as.numeric(employee$DepartmentID)
merge(employee,department)
于 2012-06-15T04:35:18.767 回答