Skip to content

Instantly share code, notes, and snippets.

@patperu
Created September 2, 2012 08:00
Show Gist options
  • Save patperu/3595729 to your computer and use it in GitHub Desktop.
Save patperu/3595729 to your computer and use it in GitHub Desktop.
Reduce and mapply
x <- data.frame(id= 1:5, wz=LETTERS[1:5], matrix(1:40, ncol=8) )
x1 <- x[, c(1:2, 3:6)]
x2 <- x[, c(1:2, 7)]
x3 <- x[, c(1:2, 8:10)]
l <- list(x1, x2, x3)
l[[3]] <- l[[3]][order(l[[3]]$id, decreasing = TRUE),]
l
# pfff
Reduce("merge", l)
# sort
#l <- lapply(l, function(m) m[order(m$id),])
m1 <- mapply(function(m, nc) m[, nc], m=l, nc=list(c(1:2, 3:6), c(1:3), c(1:2, 3:4)))
m1
do.call("cbind", m1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment