gpt4 book ai didi

r - group_by(across(all_of(vars, YEARS))) - 按具有固定 YEAR 变量的变量分组

转载 作者:行者123 更新时间:2023-12-02 16:11:16 24 4
gpt4 key购买 nike

我有一些数据想总结一下。我想对所有列进行总结,固定 YEAR 列。即对于一个变量我可以做:

df %>% 
group_by(LG1, YEAR) %>%
summarise(Freq = n())

但是我想对每个变量都这样做。以下内容无法按我的意愿工作,因为它没有按 YEAR 变量分组。我试图包含返回错误的 group_by(across(all_of(c(vars, YEAR)))) %>%

vars <- c("LG1", "AA1", "FNB1", "RE1", "PE1", "LG2", "AA2", "FNB2", "RE2", "PE2", "LG3", "AA3", "FNB3", "RE3", "PE3")
df %>%
select(c(all_of(vars), "YEAR")) %>%
group_by(across()) %>%
summarise(Freq = n())

预期输出将是一个数据框,其中包含每个变量按年份显示的频率。

数据:

df <- structure(list(ï..N.QUESTIONAIRE = c(119L, 122L, 137L, 59L, 121L, 
19L, 50L, 40L, 124L, 108L, 26L, 193L, 94L, 27L, 49L, 82L, 149L,
88L, 133L, 150L, 5L, 28L, 175L, 91L, 151L, 97L, 70L, 42L, 21L,
155L), LG1 = c(4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 3L, 4L, 5L,
4L, 4L, 4L, 5L, 4L, 4L, 5L, 4L, 5L, 4L, 4L, 3L, 5L, 5L, 5L, 3L,
3L, 3L), AA1 = c(1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 3L,
1L, 1L, 3L, 2L, 1L, 1L, 3L, 1L, 3L, 1L, 2L, 3L, 2L, 2L, 2L, 2L,
2L, 4L, 1L), FNB1 = c(4L, 4L, 5L, 4L, 4L, 4L, 4L, 4L, 3L, 4L,
4L, 5L, 4L, 4L, 4L, 5L, 4L, 4L, 4L, 5L, 5L, 4L, 4L, 4L, 5L, 2L,
5L, 4L, 3L, 4L), RE1 = c(2L, 3L, 1L, 2L, 1L, 3L, 3L, 2L, 1L,
3L, 3L, 4L, 1L, 2L, 3L, 2L, 2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1L, 1L, 4L, 2L, 4L, 4L, 1L, 1L, 3L, 2L), FNB3 = c(5L,
5L, 5L, 5L, 5L, 2L, 4L, 4L, 5L, 4L, 5L, 5L, 4L, 4L, 5L, 5L, 5L,
4L, 5L, 5L, 5L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), RE3 = c(2L,
2L, 2L, 2L, 3L, 4L, 4L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 2L, 1L,
4L, 4L, 1L, 3L, 1L, 1L, 3L, 5L, 1L, 2L, 4L, 3L, 2L), PE3 = c(5L,
3L, 4L, 4L, 4L, 4L, 3L, 4L, 5L, 5L, 4L, 4L, 4L, 4L, 3L, 4L, 4L,
4L, 4L, 4L, 5L, 4L, 4L, 3L, 5L, 5L, 4L, 3L, 4L, 3L), YEAR = c(2L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L,
1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L), NATIONALITY = c(2L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 3L, 1L, 2L, 1L, 1L,
3L, 1L, 1L, 2L, 2L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 1L), GENDER = c("F",
"F", "M", "M", "F", "M", "M", "F", "F", "M", "F", "M", "M", "F",
"M", "F", "F", "F", "M", "F", "M", "F", "M", "M", "F", "M", "M",
"F", "M", "F"), AGE = c(1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 3L,
1L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 1L, 2L, 2L, 3L, 2L, 3L,
2L, 2L, 1L, 2L)), class = "data.frame", row.names = c(NA, -30L
))

最佳答案

我们可以在 group_by 中使用 across 来包含所有 vars 列以及 YEAR

library(dplyr)

df %>% group_by(across(c(all_of(vars), "YEAR"))) %>% summarise(Freq = n())

我们也可以在这里使用count -

df %>% count(across(c(all_of(vars), "YEAR")))

如果您想对 vars 中的每个值进行计数,您可以使用 map

purrr::map(vars, ~df %>% count(YEAR, .data[[.x]]))

关于r - group_by(across(all_of(vars, YEARS))) - 按具有固定 YEAR 变量的变量分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67958155/

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