【发布时间】:2020-07-04 11:50:08
【问题描述】:
我有一个包含“性别”和“经济”列的数据集,其中包含以下观察结果:
gender economy
1 Male Bad
2 Female Bad
3 Female Bad
4 Male Bad
5 Male Good
6 Male Bad
7 Male Very bad
8 Male Very bad
9 Male Very bad
10 Male Very bad
11 Female Bad
12 Male Good
13 Male Good
14 Female Good
15 Male Bad
16 Female Good
17 Female Very bad
18 Male Very bad
19 Female Good
20 Female Bad
structure(list(gender = structure(c(2L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L), .Label = c("Female",
"Male"), class = "factor"), economy = structure(c(3L, 3L, 3L,
3L, 2L, 3L, 4L, 4L, 4L, 4L, 3L, 2L, 2L, 2L, 3L, 2L, 4L, 4L, 2L,
3L), .Label = c("Very good", "Good", "Bad", "Very bad", "Don't know"
), class = "factor")), row.names = c(NA, 20L), class = "data.frame")
我现在想计算女性和男性的比例,以及受访者表示经济不好或非常糟糕的总和值。我可以在 R 之外手动计算它,但我想知道在 R 中快速计算它的方法。我知道如何计算份额,但现在我被困住了:
lebanon %>%
group_by(gender) %>%
filter(!is.na(economy), economy != "Don't know") %>%
count(economy) %>%
mutate(prop = n / sum(n) * 100)
gender economy n prop
<fct> <fct> <int> <dbl>
1 Female Very good 7 0.586
2 Female Good 146 12.2
3 Female Bad 544 45.6
4 Female Very bad 497 41.6
5 Male Very good 5 0.417
6 Male Good 161 13.4
7 Male Bad 515 42.9
8 Male Very bad 519 43.2
问候
【问题讨论】:
-
认为经济不景气或非常糟糕的受访者百分比将由
100 * length(grep("B|bad", lebanon$economy))/nrow(lebanon)给出 -
很好,谢谢!有没有另一种方法可以让我得到每个性别的结果,就像我在这里发布的表格中一样?所以我有四行,每种性别两行,总和值为“非常好/好”和“非常坏/坏”。这将使它更容易与 ggplot 一起使用。
-
嗨 Nicosc - 是的。我添加了一个答案