我想合并两个数据帧。 这是我的数据帧
df1 =
id effectiveDate value
1 2019-01-01 0.1
1 2019-01-02 0.2
df2 =
id name
1 abc
我希望我的最终数据框看起来像 df =
id name column3
1 abc 2019-01-01, 2019-01-02, 0.1, 0.2
但是我得到的是
df =
id name effectiveDate value
1 abc 2019-01-01 0.1
1 abc 2019-01-02 0.2
答案 0 :(得分:0)
您可以使用dplyr
来完成它:
library(dplyr)
df2 %>%
left_join(df1, by = "id") %>%
group_by(id, name) %>%
summarise(column3 = paste(toString(effectiveDate), toString(value), sep = ", "))
## A tibble: 1 x 3
## Groups: id [1]
#id name column3
# <int> <fct> <chr>
#1 1 abc 2019-01-01, 2019-01-02, 0.1, 0.2
如果要避免使用组,可以执行以下操作:
df2 %>%
left_join(df1, by = "id") %>%
mutate(column3 = paste(toString(effectiveDate), toString(value), sep = ", ")) %>%
select(id, name, column3) %>%
distinct()
# id name column3
#1 1 abc 2019-01-01, 2019-01-02, 0.1, 0.2