简体   繁体   English

根据R中其余的唯一性汇总一列

[英]aggregate one column based on unique of the rest in R

I want to aggregate the freq column based on the unique of the rest of the columns. 我想基于其余列的唯一性来聚集freq列。 I usually use 我通常使用

 aggregate(freq~var1+var2+var3, df, sum)

but in this case, the variables would be unknown as the user would select variables using a drop down in shiny 但是在这种情况下,变量将是未知的,因为用户将使用闪亮的下拉菜单选择变量

DISEASE Gender  Age Race    Date       Freq
Campy       F   32  ASIAN   1/6/2014    10
Campy       M   52  WHITE   1/2/2014    15
Campy       M   63  WHITE   1/3/2014    56
Chlamydia   F   24  LATINO  1/8/2014    32
Chlamydia   F   32  AA      1/3/2014    52

So in my case the user would select either of the first four columns, for instance, someone may just select the Disease, then I need to aggregate frequencies by disease and someone may select disease and gender and likewise 因此,在我的情况下,用户将选择前四列中的任意一列,例如,某人可能只是选择疾病,然后我需要按疾病汇总频率,某人可能会选择疾病和性别

User supplies column names to aggregate as a vector: 用户提供要汇总为矢量的列名称:

user.cols <- c("DISEASE",'Age')

You create a unique vector in the dataframe by these columns 您可以通过这些列在数据框中创建唯一的矢量

df$unq <- apply( df[ , user.cols ] , 1 , paste , collapse = "-" )

go ahead and aggregate by the unique column: 继续按照唯一列进行汇总:

output <- aggregate(freq~unq, df, sum)

Consider a dynamic formula built with paste(..., collapse=...) : 考虑使用paste(..., collapse=...)构建的动态公式:

dynamic_agg <- function(vars) {
  f <- as.formula(paste0("Freq~", paste(vars, collapse="+")))
  aggregate(f, df, FUN=sum)
}  

dynamic_agg(c("Date"))
#       Date Freq
# 1 1/2/2014   15
# 2 1/3/2014  108
# 3 1/6/2014   10
# 4 1/8/2014   32

dynamic_agg(c("Gender", "Date"))
#   Gender     Date Freq
# 1      M 1/2/2014   15
# 2      F 1/3/2014   52
# 3      M 1/3/2014   56
# 4      F 1/6/2014   10
# 5      F 1/8/2014   32

dynamic_agg(c("DISEASE", "Gender", "Date"))
#     DISEASE Gender     Date Freq
# 1     Campy      M 1/2/2014   15
# 2 Chlamydia      F 1/3/2014   52
# 3     Campy      M 1/3/2014   56
# 4     Campy      F 1/6/2014   10
# 5 Chlamydia      F 1/8/2014   32

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

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