Is it possible to chain multiple merge operations one after another with data.tables
?
The functionality would be similar to joining multiple data.frames
in a dplyr
pipe but would be used for data.tables
in a similar chained fashion as merging two data.tables
in the below and then manipulating the data.table
as required. But only you would be then able to merge another data.table
. I am acknowledging this SO question here may be very similar, that is after @chinsoon12 posted the comment.
Thanks for any help!
library(dplyr)
library(data.table)
# data.frame
df1 = data.frame(food = c("apples", "bananas", "carrots", "dates"),
quantity = c(1:4))
df2 = data.frame(food = c("apples", "bananas", "carrots", "dates"),
status = c("good", "bad", "rotten", "raw"))
df3 = data.frame(food = c("apples", "bananas", "carrots", "dates"),
rank = c("okay", "good", "better", "best"))
df4 = left_join(df1,
df2,
by = "food") %>%
mutate(new_col = NA) %>% # this is just to hold a position of mutation in the data.frame
left_join(.,
df3,
by = "food")
# data.table
dt1 = data.table(food = c("apples", "bananas", "carrots", "dates"),
quantity = c(1:4))
dt2 = data.table(food = c("apples", "bananas", "carrots", "dates"),
status = c("good", "bad", "rotten", "raw"))
dt3 = data.table(food = c("apples", "bananas", "carrots", "dates"),
rank = c("okay", "good", "better", "best"))
# this is what I am not sure how to implement
dt4 = merge(dt1,
dt2,
by = "food")[
food == "apples"](merge(dt4))