将三个数据框的内容合并到一列

时间:2019-07-26 11:09:54

标签: r dataframe merge

我有以下三个data.frame

 df1 <- data.frame(c("A", "B", "C", "D"), 
      c("text1", "texta", "textk", "textx"))
 names(df1) <- c('dummy_1', 'dummy_2')
 df2 <- data.frame(c("A", "B", "C", "D"), 
      c("text2", "textb", "textl", "texty"))
 names(df2) <- c('dummy_1', 'dummy_3')
 df3 <- data.frame(c("A", "B", "C", "D"), 
      c("text3", "textc", "textm", "textz"))
 names(df3) <- c('dummy_1', 'dummy_4')

如何合并dummy_2dummy_3dummy_4df1df2df3列中的文本,分成一列,用" \n "分隔?所以理想的结果就是这个data.frame:

 dummy_1    merged
 A          text1 \n text2 \n text3
 B          texta \n textb \n textc
 C          textk \n textl \n textm
 D          textx \n texty \n textz

4 个答案:

答案 0 :(得分:3)

可以尝试:

library(tidyverse)

list(df1, df2, df3) %>%
  reduce(left_join) %>%
  unite(merged, -dummy_1, sep = " \n ")

输出:

  dummy_1                  merged
1       A text1 \n text2 \n text3
2       B texta \n textb \n textc
3       C textk \n textl \n textm
4       D textx \n texty \n textz

答案 1 :(得分:2)

使用merge

step1 <- merge(merge(df1, df2, by='dummy_1'), df3, by='dummy_1')
result <- transform(step1, merged=paste(dummy_2, dummy_3, dummy_4, sep='\n'))

在旁注中,您不必随后添加names

df1 <- data.frame(dummy_1 = c("A", "B", "C", "D"), 
  dummy_2 = c("text1", "texta", "textk", "textx"))

答案 2 :(得分:2)

合并所有三个数据帧(您可以使用cbind()$引用,但如果dummy_1以不同顺序列出值,则合并会更安全):

df4 <- Reduce(merge, list(df1,df2,df3))

(在生产代码中,您可能希望像@MrGumble的答案中那样显式指定by,但是令人讨厌的Reduce()不需要使用...参数,因此您需要定义一个匿名函数)

paste()的值和" \n "的分隔符(transform()是装饰性的,在生产代码中可能不建议使用):

df4 <- transform(df4,merged=paste(dummy_2,dummy_3,dummy_4,sep=" \n "))

仅保留所需的列:

df4 <- df4[c("dummy_1","merged")]

答案 3 :(得分:2)

pasted <- paste(df1$dummy_2, df2$dummy_3, df3$dummy_4, sep = " \n ")

df_pasted <- data.frame(dummy_1 = df1$dummy_1, merged = pasted)