Skip to content

Commit

Permalink
Simplify join code and improve example
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 1, 2010
1 parent 777c6d1 commit 49483bf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
12 changes: 5 additions & 7 deletions R/join.r
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#' first <- ddply(baseball, "id", summarise, first = min(year))
#' system.time(b2 <- merge(baseball, first, by = "id", all.x = TRUE))
#' system.time(b3 <- join(baseball, first, by = "id"))
#'
#' b2 <- arrange(b2, id, year, stint)
#' b3 <- arrange(b3, id, year, stint)
#' stopifnot(all.equal(b2, b3))
join <- function(x, y, by = intersect(names(x), names(y)), type = "left") {
type <- match.arg(type, c("left", "right", "inner", "full"))

Expand Down Expand Up @@ -60,13 +64,7 @@ join <- function(x, y, by = intersect(names(x), names(y)), type = "left") {
#' @keywords internal
join.keys <- function(x, y, by) {
joint <- rbind.fill(x[by], y[by])

factors <- sapply(joint, is.factor)
joint[!factors] <- lapply(joint[!factors], function(x) {
factor(x, levels = unique(x))
})

keys <- ninteraction(joint)
keys <- id(joint)

list(
x = keys[1:nrow(x)],
Expand Down
6 changes: 5 additions & 1 deletion man/join.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@
}
\examples{first <- ddply(baseball, "id", summarise, first = min(year))
system.time(b2 <- merge(baseball, first, by = "id", all.x = TRUE))
system.time(b3 <- join(baseball, first, by = "id"))}
system.time(b3 <- join(baseball, first, by = "id"))

b2 <- arrange(b2, id, year, stint)
b3 <- arrange(b3, id, year, stint)
stopifnot(all.equal(b2, b3))}

0 comments on commit 49483bf

Please sign in to comment.