Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How can I integrate these data frames?

Tags:

dataframe

r

I have 2 data frames I would like to integrate.

Data frame 1 (Terminals) consists of the following:

peertermid <- c(1,2,3,4)
peertermlatt <- c(23,34,56,44)
peertermlong <- c(23,34,56,44)
Terminals <- data.frame(peertermid, peertermlatt, peertermlong)

Data frame 2 (Lanes) consists of the following:

laneid <- c(1,2,3,4)
olatt <- c(23,34,56,44)
olong <- c(23,34,56,44)
dlatt <- c(23,34,56,44)
dlong <- c(23,34,56,44)
Lanes <- data.frame(laneid, olatt, olong, dlatt, dlong)

I would like to combine these two data frames to get the following Master data frame:

mlaneid <- c(1)
mpeertermid <- c(1)
mpeertermlatt <- c(22)
mpeertermlong <- c(22)
molatt <- c(22)
molong <- c(22)
mdlatt <- c(22)
mdlong <- c(22)
master <- data.frame(mlaneid, mpeertermid, mpeertermlatt, mpeertermlong, molatt, molong, mdlatt, mdlong)

Master data frame should be organized so that for each row in the Lanes data frame, I am adding all of the relevant rows from the Terminals data frame.

like image 209
D. Kent Avatar asked Nov 19 '25 11:11

D. Kent


1 Answers

Is this what you want?

merge(x = Terminals, y = Lanes, by.y ="laneid", by.x = "peertermid", all = TRUE)

#Output
#   peertermid peertermlatt peertermlong olatt olong dlatt dlong
# 1          1           23           23    23    23    23    23
# 2          2           34           34    34    34    34    34
# 3          3           56           56    56    56    56    56
# 4          4           44           44    44    44    44    44
like image 50
deepseefan Avatar answered Nov 22 '25 00:11

deepseefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!