将来自三个数据框的内容合并为一列 [英] Merge contents from three dataframes into one column

查看:132
本文介绍了将来自三个数据框的内容合并为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下三个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')

如何将df1df2df3中的dummy_2dummy_3dummy_4列中的文本分别合并到由" \n "分隔的一列中?因此,理想的结果是此data.frame:

How can I merge the text from the columns dummy_2, dummy_3 and dummy_4 in df1, df2 and df3, respectively, into one column, separated by " \n "? So the desired outcome is this 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

推荐答案

可以尝试:

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

这篇关于将来自三个数据框的内容合并为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆