总结使用 dplyr 和 for 循环

Aas*_*mar 2 for-loop group-by r dplyr summarize

我想使用dplyrfor 循环来总结每个自变量(列)和目标变量。这是我的主要数据框:

  Contract_ID Asurion Variable_1 Variable_2 Variable_3
         1 年 acf
         2 年平均
         3N BCG
         4 N adf
         5 年 bcf
         6 Y adf

分组后我得到

a1 <- a %>% 
  group_by(Asurion,BhvrBnk_Donates_to_Env_Causes) %>%       
  summarise(counT=n_distinct(CONTRACT_ID)) %>%                                        
  mutate(perc=paste0(round(counT/sum(counT)*100,2),"%"))

 Asurion Variable_1 CounT   perc
    Y         a        3     75%
    Y         b        1     25%
    N         a        1     50%
    N         b        1     50%
Run Code Online (Sandbox Code Playgroud)

我希望对数据框中存在的每个变量进行汇总,并且我想使用 for 循环来完成此操作。我怎样才能达到我想要的结果

这是我尝试使用的,但似乎不起作用。这是一个学校项目,我需要为此使用 for 循环。请在这里帮助我

categorical <- colnames(a)###where categroical is the names of all columns in a  
###I would like to have a for loop for every column in a and summarise in the following way. I would like to store each of the summarisations in a separate dataframe 

for (i in categorical) {
  a[[i]] <- a %>% 
     group_by(Asurion,get(i)) %>% 
    summarise(counT=n_distinct(CONTRACT_ID)) %>% 
    mutate(perc=paste0(round(counT/sum(counT)*100,2),"%"))
  }
Run Code Online (Sandbox Code Playgroud)

Zhi*_*ang 5

你可能并不真正需要for loop得到你想要的东西。

df<-data.frame(contract_ID = 1:6, 
               Asurion = c("Y", "Y", "N", "N", "Y", "Y"), 
               Variable_1 = c("a", "a", "b", "a", "b","a"), 
               Variable_2 = c("c", "d", "c", "d", "c", "d"), 
               Variable_3 = c("f", "g", "g", "f", "f", "f"))

pct <- function(x) {
  df %>% 
  group_by(Asurion, {{x}}) %>% 
  summarise(counT=n_distinct(contract_ID)) %>% 
  mutate(perc = paste0(round(counT/sum(counT)*100,2),"%"))
}

pct(Variable_1)
pct(Variable_2)
pct(Variable_3)
Run Code Online (Sandbox Code Playgroud)

如果你确实有很多变量,你可以使用类似for loop或 的东西apply来迭代最后一位。这是一种选择:

categorical<- df[3:5]
a <- list()
j = 1
for (i in categorical) {
  a[[j]] <- df %>% 
    group_by(Asurion, {{i}}) %>% 
    summarise(counT=n_distinct(contract_ID)) %>% 
    mutate(perc = paste0(round(counT/sum(counT)*100,2),"%"))
  j = j + 1
}
a
Run Code Online (Sandbox Code Playgroud)
[[1]]
# A tibble: 4 x 4
# Groups:   Asurion [2]
  Asurion `<fct>` counT perc 
  <fct>   <fct>   <int> <chr>
1 N       a           1 50%  
2 N       b           1 50%  
3 Y       a           3 75%  
4 Y       b           1 25%  

[[2]]
# A tibble: 4 x 4
# Groups:   Asurion [2]
  Asurion `<fct>` counT perc 
  <fct>   <fct>   <int> <chr>
1 N       c           1 50%  
2 N       d           1 50%  
3 Y       c           2 50%  
4 Y       d           2 50%  

[[3]]
# A tibble: 4 x 4
# Groups:   Asurion [2]
  Asurion `<fct>` counT perc 
  <fct>   <fct>   <int> <chr>
1 N       f           1 50%  
2 N       g           1 50%  
3 Y       f           3 75%  
4 Y       g           1 25%  
Run Code Online (Sandbox Code Playgroud)

编辑 添加变量名称作为新变量值以响应您的问题以识别group_by变量。

categorical<- df[3:5]
vnames <- colnames(categorical)
a <- list()
j = 1
for (i in categorical) {
  a[[j]] <- df %>% 
    group_by(Asurion, {{i}}) %>% 
    summarise(counT=n_distinct(contract_ID)) %>% 
    mutate(perc = paste0(round(counT/sum(counT)*100,2),"%"))
    a[[j]]$vnames = vnames[j]
  j = j + 1
}
a
Run Code Online (Sandbox Code Playgroud)