gpt4 book ai didi

r - 总结使用带有 for 循环的 dplyr

转载 作者:行者123 更新时间:2023-12-01 21:54:34 28 4
gpt4 key购买 nike

我想在 for 循环中使用 dplyr 将我的每个独立变量(列)与我的目标变量进行总结。这是我的主要数据框:

  contract_ID    Asurion         Variable_1     Variable_2  Variable_3         1          Y                a               c          f         2          Y                a               d          g         3          N                b               c          g         4          N                a               d          f         5          Y                b               c          f         6          Y                a               d          f

After the group by I get

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%

我想对我的数据框中存在的每个变量进行汇总,并且我想使用 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),"%"))
}

最佳答案

你可能真的不需要 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)

如果您确实有很多变量,您可以使用类似for loopapply 的方法来迭代最后一位。这是一个选项:

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
[[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%

编辑添加变量名称作为新变量值以响应您的问题以识别 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

关于r - 总结使用带有 for 循环的 dplyr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58677491/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com