[英]r - 'rbind' dataframes with different prefix in column names
I have two dataframes like the following: 我有两个数据帧,如下所示:
df1 <- data.frame(ID = c(1:4),
Year = 2001,
a_Var1 = c("A","B","C","D"),
a_Var2 = c("T","F","F","T"))
df2 <- data.frame(ID = c(1:4),
Year = 2002,
b_Var1 = c("E","F","G","H"))
The desired end product is 期望的最终产品是
df_combined <- data.frame(ID = c(1,1,2,2,3,3,4,4),
Year = c(2001,2002,2001,2002,2001,2002,2001,2002),
Var1 = c("A","E","B","F","C","G","D","H"),
Var2 = c("T",NA,"F",NA,"F",NA,"T",NA))
Question is how to 'rbind' in such a way that the prefix a_
or b_
is removed and Var1
, Var2
, etc become the new columns. 问题是如何以删除前缀a_
或b_
并且Var1
, Var2
等成为新列的方式'rbind'。
Tried plyr
's rbind.fill
but that doesn't solve the problem. 试过plyr
的rbind.fill
但这并没有解决问题。
Here is one option. 这是一个选择。 Place the datasets in a list
, rename
by removing the prefix part including the _
and arrange
by 'ID' 将数据集放在list
,通过删除包含_
的前缀部分rename
,并按'ID' arrange
library(tidyverse)
map_df(list(df1, df2), ~ .x %>%
rename_all(~ str_remove(.x, "^[^_]+_"))) %>%
arrange(ID)
# ID Year Var1 Var2
#1 1 2001 A T
#2 1 2002 E <NA>
#3 2 2001 B F
#4 2 2002 F <NA>
#5 3 2001 C F
#6 3 2002 G <NA>
#7 4 2001 D T
#8 4 2002 H <NA>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.