我想使用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 …Run Code Online (Sandbox Code Playgroud)