我想合并2个数据框,其中每个数据框具有相同的列名Date_Time但行数不同。我想合并这些取决于Date_Time,在这里我希望所有的行应该包括如果列值(Date_Time)相等,我得到定向值(cmpny_name,Price)否则我得到NA。 Ex Dateset是, DF1:
Cmny_Name Date_Time Price
A 1/1/2015 13:27 1083
B 1/1/2015 13:28 1084
C 1/1/2015 13:29 1053
D 1/1/2015 13:31 1063
E 1/1/2015 13:33 1033
DF2:
Cmny_Name1 Date_Time Price
A 1/1/2015 13:27 1043
A 1/1/2015 13:28 1053
A 1/1/2015 13:29 1054
A 1/1/2015 13:35 1084
我的预期输出(df3)
Date_Time Cmny_Name Price Cmny_Name1 Price
1/1/2015 13:27 A 1083 A 1043
1/1/2015 13:28 B 1084 A 1053
1/1/2015 13:29 C 1053 A 1054
1/1/2015 13:31 D 1063 A NA
1/1/2015 13:33 E 1033 A NA
1/1/2015 13:35 NA NA A 1084
答案 0 :(得分:0)
df3 <- merge(df1, df2, by = "Date_Time", all = TRUE)
或dplyr
方式:
library(dplyr)
df3 <- df1 %>%
full_join(df2, by = "Date_Time")
答案 1 :(得分:0)
您可以使用基本功能merge
进行合并:
## Your data:
df1 <- structure(list(Cmny_Name = c("A", "B", "C", "D", "E"),
Date_Time = c("1/1/2015 13:27", "1/1/2015 13:28",
"1/1/2015 13:29", "1/1/2015 13:31",
"1/1/2015 13:33"),
Price = c(1083L, 1084L, 1053L, 1063L, 1033L)),
.Names = c("Cmny_Name", "Date_Time", "Price"),
class = "data.frame", row.names = c(NA, -5L))
df2 <- structure(list(Cmny_Name1 = c("A", "A", "A", "A"),
Date_Time = c("1/1/2015 13:27", "1/1/2015 13:28",
"1/1/2015 13:29", "1/1/2015 13:35"),
Price = c(1043L, 1053L, 1054L, 1084L)),
.Names = c("Cmny_Name1", "Date_Time", "Price"),
class = "data.frame", row.names = c(NA, -4L))
## Merging the two data frames:
df3 <- merge(x = df1, y = df2, by = "Date_Time", all = TRUE)
df3
# Date_Time Cmny_Name Price.x Cmny_Name1 Price.y
# 1 1/1/2015 13:27 A 1083 A 1043
# 2 1/1/2015 13:28 B 1084 A 1053
# 3 1/1/2015 13:29 C 1053 A 1054
# 4 1/1/2015 13:31 D 1063 <NA> NA
# 5 1/1/2015 13:33 E 1033 <NA> NA
# 6 1/1/2015 13:35 <NA> NA A 1084
或者如果你想要你可以,例如使用dplyr
包中的连接函数:
df3 <- dplyr::full_join(df1, df2, by = "Date_Time")
df3
# Cmny_Name Date_Time Price.x Cmny_Name1 Price.y
# 1 A 1/1/2015 13:27 1083 A 1043
# 2 B 1/1/2015 13:28 1084 A 1053
# 3 C 1/1/2015 13:29 1053 A 1054
# 4 D 1/1/2015 13:31 1063 <NA> NA
# 5 E 1/1/2015 13:33 1033 <NA> NA
# 6 <NA> 1/1/2015 13:35 NA A 1084
注意:由于生成的数据框架需要具有唯一的列名称,因此在合并期间,Price
列将重命名为Pice.x
和Price.y
。