为什么count()、filter()和mutate()在for循环中不起作用

nzkunb0c  于 2023-03-10  发布在  其他
关注(0)|答案(2)|浏览(143)

我知道这是一个愚蠢的问题,但仍然。我想 * 理解 * 为什么count()不能在这个非常简单的for循环中工作

编辑2

我用ch_data$QUnlist <- unlist(Q)解决了这个问题,但我仍然想知道为什么它能工作,是否有更好的方法

编辑1

现在我遇到了与filter()mutate()相同的问题

  • 出现错误的代码:
listDFs <- list()

for (i in 1:2) {
  
## select colums:
  
Q <- names(ch_data[, 1:2][i])

## put into dataframes: 
  
listDFs[[i]] <-  ch_data %>% 
                     dplyr::count(Q) %>% 
                  mutate(prop = round((prop.table(n) * 100), digits = 2),
                  sd = round(sd(prop.table(n)), digits = 2)) %>%
           arrange(Q)
  
}

Error in `dplyr::count()`:
! Must group by variables found in `.data`.
x Column `Q` is not found.

## edit ##

ch_data %>% 
select(Q) %>% 
 filter(!Q %in% 'Não oferta') %>% ## NOT WORKING
 count(!!!syms(Q)) %>% 
  mutate(prop = round((prop.table(n) * 100), digits = 2),
         sd = round(sd(prop.table(n)), digits = 2),
         CH = Q, ### NOT WORKING
         QUESTION = rep('mylabel', length(CH))) %>% select (-Q) %>%
  arrange(CH) %>% 
  relocate(QUESTION, CH, .before = everything())
  • 我已经试过了

答:由于我知道count()需要 Dataframe ,所以我执行了Q <- ch_data[, 1:2][i]
B)我想可能是引号的问题,所以我在count()中尝试了str_glue('{noquote(Q)}'),但它也不起作用。
这个问题和this类似,但我还是解不出来。

  • 双变量数据

(it有更多的问题,但我想让它简单化):

dput(ch_data)
structure(list(Q29 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 1L, 5L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("Não oferta", 
"1h - 3h", "Mais de 10h", "50% em LA", "100% em LA"), class = "factor"), 
    Q30 = structure(c(4L, 8L, 3L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 
    1L, 2L, 5L, 1L, 1L, 1L, 7L, 7L, 1L, 1L, 1L, 1L, 5L, 1L, 1L, 
    2L, 1L, 6L, 2L, 8L, 8L, 2L, 8L, 3L, 7L, 7L, 1L, 2L, 2L, 8L, 
    3L, 2L, 1L, 8L, 3L, 1L, 1L, 1L, 1L, 6L, 3L), .Label = c("Não oferta", 
    "1h - 3h", "4h - 5h", "6h - 8h", "9h - 10h", "Mais de 10h", 
    "50% em LA", "100% em LA"), class = "factor")), row.names = c(NA, 
-51L), class = "data.frame")
eoxn13cs

eoxn13cs1#

您可以:

library(tidyverse)
ch_data %>%
  dplyr::count(!!!syms(Q))

示例:

Q <- names(ch_data[,1:2])

ch_data %>%
  count(!!!syms(Q))

                 Q29               Q30  n
1  N\303\243o oferta N\303\243o oferta 21
2  N\303\243o oferta           1h - 3h  9
3  N\303\243o oferta           4h - 5h  4
4  N\303\243o oferta           6h - 8h  1
5  N\303\243o oferta          9h - 10h  1
6  N\303\243o oferta       Mais de 10h  1
7  N\303\243o oferta         50% em LA  2
8  N\303\243o oferta        100% em LA  5
9            1h - 3h           1h - 3h  1
10           1h - 3h           4h - 5h  1
11       Mais de 10h       Mais de 10h  1
12         50% em LA         50% em LA  2
13        100% em LA          9h - 10h  1
14        100% em LA        100% em LA  1
pbpqsu0x

pbpqsu0x2#

我想您最好避免使用programming with dplyr,在本例中:

  • 第一,变成长格式。
  • 其次,使用分组进行计算(您可能不需要prop.table
  • 第三,拆分成想要的列表。
library(tidyr)
library(dplyr) # Please update to v. >= 1.1.0

ch_data |>
  pivot_longer(starts_with("Q")) |>
  count(name, value) |>
  mutate(prop = round((prop.table(n) * 100), digits = 2),
         sd = round(sd(prop.table(n)), digits = 2),
         .by = name) |> # If dplyr v. < 1.1.0, use group_by()-notation
  group_split(name)

(You如果您需要,可以在value上使用过滤功能删除“Não oferta”,而在您需要的arrange上使用过滤功能删除“Não oferta”,例如nameprop
输出:

# A tibble: 5 × 5
  name  value           n  prop    sd
  <chr> <fct>       <int> <dbl> <dbl>
1 Q29   Não oferta     44 86.3   0.37
2 Q29   1h - 3h         2  3.92  0.37
3 Q29   Mais de 10h     1  1.96  0.37
4 Q29   50% em LA       2  3.92  0.37
5 Q29   100% em LA      2  3.92  0.37

[[2]]
# A tibble: 8 × 5
  name  value           n  prop    sd
  <chr> <fct>       <int> <dbl> <dbl>
1 Q30   Não oferta     21 41.2   0.13
2 Q30   1h - 3h        10 19.6   0.13
3 Q30   Mais de 10h     2  3.92  0.13
4 Q30   50% em LA       4  7.84  0.13
5 Q30   100% em LA      6 11.8   0.13
6 Q30   4h - 5h         5  9.8   0.13
7 Q30   6h - 8h         1  1.96  0.13
8 Q30   9h - 10h        2  3.92  0.13

相关问题