首页 > 解决方案 > 绑定行不考虑变量名

问题描述

我有几个共享相同结构但具有不同列名的数据框。我想将它们全部合并到一个数据框中,但如果我使用bind_rows()它会创建新的列名。

我尝试了smartbind(),union()union_all()其他库,但是,它们都不能简单地合并它们。

这里有一些示例数据:

df1 <- structure(list(Codigo_Cliente = c(292640L, 48296L, 28368L, 27631L, 
21715L, 401076L), Segmento = structure(c(3L, 3L, 3L, 3L, 3L, 
5L), .Label = c("Clasico", "Emergente", "Mi_Negocio", "Preferencial", 
"Prestige"), class = "factor"), Sal_Cons_CA_2018 = c(115966976.4748, 
41404074.5338, 21576406.4326, NA, 5217387.0461, NA), Sal_Cons_CA_2019 = c(233057582.7658, 
146012775.8314, 121273292.4548, 72383484.8781, 76605696.1462, 
64418761.5503), Tipo_Cliente = structure(c(2L, 2L, 2L, 2L, 2L, 
1L), .Label = c("Nuevo", "Viejo"), class = "factor"), diferencia_anual = c(117090606.291, 
104608701.2976, 99696886.0222, 72383484.8781, 71388309.1001, 
64418761.5503), peso_cambio = c(11.7925653553277, 10.5354732191076, 
10.040788765049, 7.28996973463426, 7.18974243396645, 6.48781725327502
), cum = c(117090606.291, 221699307.5886, 321396193.6108, 393779678.4889, 
465167987.589, 529586749.1393), cum_cambio = c(11.7925653553277, 
22.3280385744352, 32.3688273394842, 39.6587970741185, 46.8485395080849, 
53.33635676136), ones = c(1, 1, 1, 1, 1, 1), clientes = c(1, 
2, 3, 4, 5, 6), porcentaje_acumulado_clientes = c(0.040650406504065, 
0.0813008130081301, 0.121951219512195, 0.16260162601626, 0.203252032520325, 
0.24390243902439), Tipo_Aportante = c("Viejo Aportante", "Viejo Aportante", 
"Viejo Aportante", "Nuevo Aportante", "Viejo Aportante", "Nuevo Aportante"
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(Codigo_Cliente = c(21715L, 27631L, 
28368L, 48296L, 292640L, 401076L), Segmento = structure(c(3L, 
3L, 3L, 3L, 3L, 5L), .Label = c("Clasico", "Emergente", "Mi_Negocio", 
"Preferencial", "Prestige"), class = "factor"), .rows = list(
    5L, 4L, 3L, 2L, 1L, 6L)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

df2 <- structure(list(Codigo_Cliente = c(29460L, 208833L, 494610L, 292653L, 
371679L, 54042L), Segmento = structure(c(3L, 3L, 3L, 3L, 3L, 
3L), .Label = c("Clasico", "Emergente", "Mi_Negocio", "Preferencial", 
"Prestige"), class = "factor"), Sal_Cons_CC_2018 = c(249412694.49, 
226519.47, NA, 232072.25, 893861.14, 2305969.41), Sal_Cons_CC_2019 = c(492333714.52, 
217220231.86, 140551673.22, 73744015.83, 57995686.81, 54669407.01
), Tipo_Cliente = structure(c(2L, 2L, 1L, 2L, 2L, 2L), .Label = c("Nuevo", 
"Viejo"), class = "factor"), diferencia_anual = c(242921020.03, 
216993712.39, 140551673.22, 73511943.58, 57101825.67, 52363437.6
), peso_cambio = c(30.7889911838579, 27.5028381525124, 17.8142024395939, 
9.31726115143663, 7.23736301995891, 6.63679667747068), cum = c(242921020.03, 
459914732.42, 600466405.64, 673978349.22, 731080174.89, 783443612.49
), cum_cambio = c(30.7889911838579, 58.2918293363703, 76.1060317759641, 
85.4232929274008, 92.6606559473597, 99.2974526248303), ones = c(1, 
1, 1, 1, 1, 1), clientes = c(1, 2, 3, 4, 5, 6), porcentaje_acumulado_clientes = c(0.0369822485207101, 
0.0739644970414201, 0.11094674556213, 0.14792899408284, 0.18491124260355, 
0.22189349112426), Tipo_Aportante = c("Viejo Aportante", "Viejo Aportante", 
"Nuevo Aportante", "Viejo Aportante", "Viejo Aportante", "Viejo Aportante"
)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L), groups = structure(list(Codigo_Cliente = c(29460L, 54042L, 
208833L, 292653L, 371679L, 494610L), Segmento = structure(c(3L, 
3L, 3L, 3L, 3L, 3L), .Label = c("Clasico", "Emergente", "Mi_Negocio", 
"Preferencial", "Prestige"), class = "factor"), .rows = list(
    1L, 6L, 2L, 4L, 5L, 3L)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE))

标签: rdplyr

解决方案


您可以使用data.table具有rbindlist以下功能的包:

df <- rbindlist(list(df1,df2), use.names = T)

推荐阅读