我知道这是一个愚蠢的问题,但仍然。我想 * 理解 * 为什么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")
2条答案
按热度按时间eoxn13cs1#
您可以:
示例:
pbpqsu0x2#
我想您最好避免使用programming with dplyr,在本例中:
prop.table
)(You如果您需要,可以在
value
上使用过滤功能删除“Não oferta”,而在您需要的arrange
上使用过滤功能删除“Não oferta”,例如name
或prop
。输出: