Select 仅数据框中与 R 中另一个数据框具有相同列名的列
Select only the columns in a dataframe which have the same column names as another dataframe in R
我有几个数据集,如下所示:
df1 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
L1_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
L2_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2),
gender = c(rep("M",5), rep("F",5)),
ID = c("A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121"))
df2 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
Z4_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
Z5_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2),
gender = c(rep("M",5), rep("F",5)),
ID = c("Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121"))
我想将所有这些数据集绑定在一起,形成一个更大的数据集。为此,我需要每个数据集都具有相同的列名。因此,我尝试对所有数据集进行子集化,使其只包含它们共有的列/变量。
这是我尝试过的方法,但是没有用。
test <- df1 %>%
select(names(df1) %in% names(df2))
我想要的输出是:
df3 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
age = c(rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2), rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2)),
gender = c(rep("M",5), rep("F",5), rep("M",5), rep("F",5)),
ID = c("A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121", "Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121") )
根据以下回复,我的代码现在很长。因为我有多个数据集,所以这非常耗时。本练习的目的是仅对所有六个数据集中共有的列进行子集化,因此我不想使用 bind_rows。
我最终编写了如下所示的迭代代码。有谁知道是否有更有效的方法来做到这一点?谢谢。
nm = intersect(names(NZHS_Y2A), names(NZHS_Y3A))
NZHS_Y2_3 <- rbind(NZHS_Y2A[nm], NZHS_Y3A[nm])
nm = intersect(names(NZHS_Y3A), names(NZHS_Y4A))
NZHS_Y3_4 <- rbind(NZHS_Y3A[nm], NZHS_Y4A[nm])
nm = intersect(names(NZHS_Y4A), names(NZHS_Y5A))
NZHS_Y4_5 <- rbind(NZHS_Y4A[nm], NZHS_Y5A[nm])
nm = intersect(names(NZHS_Y5A), names(NZHS_Y6A))
NZHS_Y5_6 <- rbind(NZHS_Y5A[nm], NZHS_Y6A[nm])
nm = intersect(names(NZHS_Y2_3), names(NZHS_Y3_4))
NZHS_Y2_4 <- rbind(NZHS_Y2_3[nm], NZHS_Y3_4[nm])
nm = intersect(names(NZHS_Y3_4), names(NZHS_Y4_5))
NZHS_Y3_5 <- rbind(NZHS_Y3_4[nm], NZHS_Y4_5[nm])
nm = intersect(names(NZHS_Y4_5), names(NZHS_Y5_6))
NZHS_Y4_6 <- rbind(NZHS_Y4_5[nm], NZHS_Y5_6[nm])
nm = intersect(names(NZHS_Y2_4), names(NZHS_Y3_5))
NZHS_Y2_5 <- rbind(NZHS_Y2_4[nm], NZHS_Y3_5[nm])
nm = intersect(names(NZHS_Y3_5), names(NZHS_Y4_6))
NZHS_Y3_6 <- rbind(NZHS_Y3_5[nm], NZHS_Y4_6[nm])
nm = intersect(names(NZHS_Y2_5), names(NZHS_Y4_6))
NZHS_Ad_2_6 <- rbind(NZHS_Y2_5[nm], NZHS_Y4_6[nm])
您可以使用 intersect
获取两个数据帧之间共有的列集,如 中所述。
另一种方法是使用 dplyr
的 bind_rows
,它允许您匹配匹配的列并用缺失值填充不匹配的列。在某些情况下,这可能是更理想的输出。
编辑:要处理许多数据帧,您应该将它们存储在列表中并使用 reduce
获取所有数据帧的交集。这会将函数应用于列表中的前两个元素,然后是该元素的结果和第三个元素,依此类推。然后你可以 map_dfr
在列表上 select 只有来自每个数据框和行的共享列将它们绑定在一起(或者 map
然后 do.call(rbind, .)
如果你想使用rbind
。在这种情况下不需要,但 bind_rows
直接接受列表作为输入。
df1 <- data.frame(
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
L1_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
L2_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"), 2),
gender = c(rep("M", 5), rep("F", 5)),
ID = c(
"A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121"
)
)
df2 <- data.frame(
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
Z4_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
Z5_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"), 2),
gender = c(rep("M", 5), rep("F", 5)),
ID = c(
"Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121"
)
)
library(tidyverse)
df_list <- list(df1, df2)
cols <- reduce(df_list, .f = ~ intersect(colnames(.x), colnames(.y)))
map_dfr(df_list, ~ .[cols])
#> A1_01 A2_01 A3_02 age gender ID
#> 1 1 1 0 40-44 M A12345
#> 2 0 1 0 45-49 M A23456
#> 3 0 1 0 50-54 M A34767
#> 4 1 0 1 55-59 M A34567
#> 5 0 1 0 60-64 M A45678
#> 6 1 0 1 40-44 F A67891
#> 7 0 0 0 45-49 F A78910
#> 8 1 0 1 50-54 F A91011
#> 9 0 0 1 55-59 F A10111
#> 10 0 0 0 60-64 F A11121
#> 11 1 1 0 40-44 M Q12345
#> 12 0 1 0 45-49 M Q23456
#> 13 0 1 0 50-54 M Q34767
#> 14 1 0 1 55-59 M Q34567
#> 15 0 1 0 60-64 M Q45678
#> 16 1 0 1 40-44 F Q67891
#> 17 0 0 0 45-49 F Q78910
#> 18 1 0 1 50-54 F Q91011
#> 19 0 0 1 55-59 F Q10111
#> 20 0 0 0 60-64 F Q11121
bind_rows(df_list)
#> A1_01 A2_01 A3_02 L1_02 L2_02 age gender ID Z4_02 Z5_02
#> 1 1 1 0 1 0 40-44 M A12345 NA NA
#> 2 0 1 0 1 0 45-49 M A23456 NA NA
#> 3 0 1 0 1 0 50-54 M A34767 NA NA
#> 4 1 0 1 1 1 55-59 M A34567 NA NA
#> 5 0 1 0 1 1 60-64 M A45678 NA NA
#> 6 1 0 1 0 1 40-44 F A67891 NA NA
#> 7 0 0 0 0 0 45-49 F A78910 NA NA
#> 8 1 0 1 1 1 50-54 F A91011 NA NA
#> 9 0 0 1 1 0 55-59 F A10111 NA NA
#> 10 0 0 0 0 0 60-64 F A11121 NA NA
#> 11 1 1 0 NA NA 40-44 M Q12345 1 0
#> 12 0 1 0 NA NA 45-49 M Q23456 1 0
#> 13 0 1 0 NA NA 50-54 M Q34767 1 0
#> 14 1 0 1 NA NA 55-59 M Q34567 1 1
#> 15 0 1 0 NA NA 60-64 M Q45678 1 1
#> 16 1 0 1 NA NA 40-44 F Q67891 0 1
#> 17 0 0 0 NA NA 45-49 F Q78910 0 0
#> 18 1 0 1 NA NA 50-54 F Q91011 1 1
#> 19 0 0 1 NA NA 55-59 F Q10111 1 0
#> 20 0 0 0 NA NA 60-64 F Q11121 0 0
由 reprex package (v0.2.0) 创建于 2018-08-01。
我有几个数据集,如下所示:
df1 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
L1_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
L2_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2),
gender = c(rep("M",5), rep("F",5)),
ID = c("A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121"))
df2 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
Z4_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
Z5_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2),
gender = c(rep("M",5), rep("F",5)),
ID = c("Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121"))
我想将所有这些数据集绑定在一起,形成一个更大的数据集。为此,我需要每个数据集都具有相同的列名。因此,我尝试对所有数据集进行子集化,使其只包含它们共有的列/变量。
这是我尝试过的方法,但是没有用。
test <- df1 %>%
select(names(df1) %in% names(df2))
我想要的输出是:
df3 <- data.frame (
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
age = c(rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2), rep(c("40-44", "45-49", "50-54", "55-59", "60-64"),2)),
gender = c(rep("M",5), rep("F",5), rep("M",5), rep("F",5)),
ID = c("A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121", "Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121") )
根据以下回复,我的代码现在很长。因为我有多个数据集,所以这非常耗时。本练习的目的是仅对所有六个数据集中共有的列进行子集化,因此我不想使用 bind_rows。
我最终编写了如下所示的迭代代码。有谁知道是否有更有效的方法来做到这一点?谢谢。
nm = intersect(names(NZHS_Y2A), names(NZHS_Y3A))
NZHS_Y2_3 <- rbind(NZHS_Y2A[nm], NZHS_Y3A[nm])
nm = intersect(names(NZHS_Y3A), names(NZHS_Y4A))
NZHS_Y3_4 <- rbind(NZHS_Y3A[nm], NZHS_Y4A[nm])
nm = intersect(names(NZHS_Y4A), names(NZHS_Y5A))
NZHS_Y4_5 <- rbind(NZHS_Y4A[nm], NZHS_Y5A[nm])
nm = intersect(names(NZHS_Y5A), names(NZHS_Y6A))
NZHS_Y5_6 <- rbind(NZHS_Y5A[nm], NZHS_Y6A[nm])
nm = intersect(names(NZHS_Y2_3), names(NZHS_Y3_4))
NZHS_Y2_4 <- rbind(NZHS_Y2_3[nm], NZHS_Y3_4[nm])
nm = intersect(names(NZHS_Y3_4), names(NZHS_Y4_5))
NZHS_Y3_5 <- rbind(NZHS_Y3_4[nm], NZHS_Y4_5[nm])
nm = intersect(names(NZHS_Y4_5), names(NZHS_Y5_6))
NZHS_Y4_6 <- rbind(NZHS_Y4_5[nm], NZHS_Y5_6[nm])
nm = intersect(names(NZHS_Y2_4), names(NZHS_Y3_5))
NZHS_Y2_5 <- rbind(NZHS_Y2_4[nm], NZHS_Y3_5[nm])
nm = intersect(names(NZHS_Y3_5), names(NZHS_Y4_6))
NZHS_Y3_6 <- rbind(NZHS_Y3_5[nm], NZHS_Y4_6[nm])
nm = intersect(names(NZHS_Y2_5), names(NZHS_Y4_6))
NZHS_Ad_2_6 <- rbind(NZHS_Y2_5[nm], NZHS_Y4_6[nm])
您可以使用 intersect
获取两个数据帧之间共有的列集,如
另一种方法是使用 dplyr
的 bind_rows
,它允许您匹配匹配的列并用缺失值填充不匹配的列。在某些情况下,这可能是更理想的输出。
编辑:要处理许多数据帧,您应该将它们存储在列表中并使用 reduce
获取所有数据帧的交集。这会将函数应用于列表中的前两个元素,然后是该元素的结果和第三个元素,依此类推。然后你可以 map_dfr
在列表上 select 只有来自每个数据框和行的共享列将它们绑定在一起(或者 map
然后 do.call(rbind, .)
如果你想使用rbind
。在这种情况下不需要,但 bind_rows
直接接受列表作为输入。
df1 <- data.frame(
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
L1_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
L2_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"), 2),
gender = c(rep("M", 5), rep("F", 5)),
ID = c(
"A12345", "A23456", "A34767", "A34567", "A45678", "A67891", "A78910", "A91011",
"A10111", "A11121"
)
)
df2 <- data.frame(
A1_01 = c(1, 0, 0, 1, 0, 1, 0, 1, 0, 0),
A2_01 = c(1, 1, 1, 0, 1, 0, 0, 0, 0, 0),
A3_02 = c(0, 0, 0, 1, 0, 1, 0, 1, 1, 0),
Z4_02 = c(1, 1, 1, 1, 1, 0, 0, 1, 1, 0),
Z5_02 = c(0, 0, 0, 1, 1, 1, 0, 1, 0, 0),
age = rep(c("40-44", "45-49", "50-54", "55-59", "60-64"), 2),
gender = c(rep("M", 5), rep("F", 5)),
ID = c(
"Q12345", "Q23456", "Q34767", "Q34567", "Q45678", "Q67891", "Q78910", "Q91011",
"Q10111", "Q11121"
)
)
library(tidyverse)
df_list <- list(df1, df2)
cols <- reduce(df_list, .f = ~ intersect(colnames(.x), colnames(.y)))
map_dfr(df_list, ~ .[cols])
#> A1_01 A2_01 A3_02 age gender ID
#> 1 1 1 0 40-44 M A12345
#> 2 0 1 0 45-49 M A23456
#> 3 0 1 0 50-54 M A34767
#> 4 1 0 1 55-59 M A34567
#> 5 0 1 0 60-64 M A45678
#> 6 1 0 1 40-44 F A67891
#> 7 0 0 0 45-49 F A78910
#> 8 1 0 1 50-54 F A91011
#> 9 0 0 1 55-59 F A10111
#> 10 0 0 0 60-64 F A11121
#> 11 1 1 0 40-44 M Q12345
#> 12 0 1 0 45-49 M Q23456
#> 13 0 1 0 50-54 M Q34767
#> 14 1 0 1 55-59 M Q34567
#> 15 0 1 0 60-64 M Q45678
#> 16 1 0 1 40-44 F Q67891
#> 17 0 0 0 45-49 F Q78910
#> 18 1 0 1 50-54 F Q91011
#> 19 0 0 1 55-59 F Q10111
#> 20 0 0 0 60-64 F Q11121
bind_rows(df_list)
#> A1_01 A2_01 A3_02 L1_02 L2_02 age gender ID Z4_02 Z5_02
#> 1 1 1 0 1 0 40-44 M A12345 NA NA
#> 2 0 1 0 1 0 45-49 M A23456 NA NA
#> 3 0 1 0 1 0 50-54 M A34767 NA NA
#> 4 1 0 1 1 1 55-59 M A34567 NA NA
#> 5 0 1 0 1 1 60-64 M A45678 NA NA
#> 6 1 0 1 0 1 40-44 F A67891 NA NA
#> 7 0 0 0 0 0 45-49 F A78910 NA NA
#> 8 1 0 1 1 1 50-54 F A91011 NA NA
#> 9 0 0 1 1 0 55-59 F A10111 NA NA
#> 10 0 0 0 0 0 60-64 F A11121 NA NA
#> 11 1 1 0 NA NA 40-44 M Q12345 1 0
#> 12 0 1 0 NA NA 45-49 M Q23456 1 0
#> 13 0 1 0 NA NA 50-54 M Q34767 1 0
#> 14 1 0 1 NA NA 55-59 M Q34567 1 1
#> 15 0 1 0 NA NA 60-64 M Q45678 1 1
#> 16 1 0 1 NA NA 40-44 F Q67891 0 1
#> 17 0 0 0 NA NA 45-49 F Q78910 0 0
#> 18 1 0 1 NA NA 50-54 F Q91011 1 1
#> 19 0 0 1 NA NA 55-59 F Q10111 1 0
#> 20 0 0 0 NA NA 60-64 F Q11121 0 0
由 reprex package (v0.2.0) 创建于 2018-08-01。