Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Libraries
- library("reshape")
- library("data.table")
- #Specify list of multiple dataframes
- filelist <- list(data.frame(x=c(1,1,1,2,2,2,3,3,3), y=c(1,2,3,1,2,3,1,2,3), a=1:9),
- data.frame(x=c(1,1,1,2,2,2,3,3,4), y=c(1,2,3,1,2,3,1,2,1), b=seq(from=0, by=5, length.out=9)),
- data.frame(x=c(1,1,1,2,2,2,3,3,4), y=c(1,2,3,1,2,3,1,2,2), c=seq(from=0, by=10, length.out=9)))
- #Merge with merge_recurse()
- listMerged <- merge_recurse(filelist, by=c("x","y"))
- #Attempt with data.table
- ids <- lapply(filelist, function(x) x[,c("x","y")])
- unique_keys <- unique(do.call("rbind", ids))
- dt <- data.table(filelist)
- setkey(dt, c("x","y")) #error here
- Reduce(function(x, y) x[y[J(unique_keys)]], filelist)
- > listMerged
- x y a b c
- 1 1 1 1 0 0
- 2 1 2 2 5 10
- 3 1 3 3 10 20
- 4 2 1 4 15 30
- 5 2 2 5 20 40
- 6 2 3 6 25 50
- 7 3 1 7 30 60
- 8 3 2 8 35 70
- 9 3 3 9 NA NA
- 10 4 1 NA 40 NA
- 11 4 2 NA NA 80
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement