简体   繁体   English

如何总结dplyr中的变量

[英]How to summarise variables in dplyr

My question is how can ii input column numbers in the vars() fucntion.我的问题是如何在vars()函数中输入列号。 If i input column positions, it doesnt work.如果我输入列位置,它不起作用。 However, if i input column names it works.但是,如果我输入列名,它会起作用。 Specifically with this code:具体使用此代码:

productos_por_profundidad <- productos_por_profundidad%>%
                             group_by(CODIGO_CLIENTE)%>%
                      summarise_at(vars(3:ncol(productos_por_profundidad)),function(x) sum((x)))

It worked with names, but that is not efficient in the long time.它适用于名称,但在很长一段时间内效率不高。

data sample数据样本

productos_por_profundidad <- structure(list(CODIGO_CLIENTE = c(12554L, 125713L, 125724L, 126095L, 
12618L, 12618L), SIGNIFICADO_ESTADO = structure(c(3L, 3L, 3L, 
3L, 3L, 3L), .Label = c("Abandonada CE", "Activa CE", "ACTIVO CD", 
"ACTIVO LEA/FAC", "ANULADO CD", "ANULADO PR", "APROBADO PR", 
"Bloqueada parcialmente CE", "Bloqueada totalmente CE", "CAMBIO DE TIPO TC/MC", 
"Cancelada CE", "CANCELADA TC/MC", "CANCELADO PR", "CASTIGADO PR", 
"COBRO JUDICIAL PR", "DESEMBOLSADO PR", "Embargada CE", "Inactiva CE", 
"LEGAL TC/MC", "MORA TC/MC", "NORMAL TC/MC", "OTROS PR", "PAGADO CD", 
"Pendiente de aprobar CE", "PERDIDA O ROBADA TC/MC", "RECHAZADO PR", 
"REESTRUCTURADO PR", "REGISTRADO PR", "RETENIDO CD", "SIN USO (3 O MAS MESES) TC/MC", 
"TARJETA NUEVA TC/MC", "TARJETAS VENCIDAS TC/MC", "Transferida al BC CE"
), class = "factor"), `Certificado en Dolares (US$)` = c(0, 0, 
0, 0, 0, 1), `Certificado en Pesos (RD$)` = c(1, 1, 1, 1, 1, 
0), `Cuentas Corrientes en Pesos (RD$)` = c(0, 0, 0, 0, 0, 0), 
    `Cuentas de Ahorro en Dólares (USD$)` = c(0, 0, 0, 0, 0, 
    0), `Cuentas de Ahorro en Euros (EUR$)` = c(0, 0, 0, 0, 0, 
    0), `Cuentas de Ahorro en Pesos (RD$)` = c(0, 0, 0, 0, 0, 
    0), Factoring = c(0, 0, 0, 0, 0, 0), Leasing = c(0, 0, 0, 
    0, 0, 0), `Linea de Credito` = c(0, 0, 0, 0, 0, 0), Multicredito = c(0, 
    0, 0, 0, 0, 0), `Nómina Electrónica` = c(0, 0, 0, 0, 0, 0
    ), `Prestamo Comercial` = c(0, 0, 0, 0, 0, 0), `Prestamo de Vehiculo` = c(0, 
    0, 0, 0, 0, 0), `Prestamo en Tienda` = c(0, 0, 0, 0, 0, 0
    ), `Prestamo Hipotecario` = c(0, 0, 0, 0, 0, 0), `Prestamo Personal` = c(0, 
    0, 0, 0, 0, 0), `Tarjeta de Credito` = c(0, 0, 0, 0, 0, 0
    )), row.names = c(NA, 6L), class = "data.frame")

You should use colnames to define your columns like that:您应该使用colnames来定义您的列:

library(dplyr)
df%>%
  group_by(CODIGO_CLIENTE)%>%
  summarise_at(vars(colnames(df)[3:ncol(df)]),function(x) sum((x)))

PS: I replace the name of your dataset by df in my example PS:在我的例子中,我用df替换了你的数据集的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM