admin管理员组文章数量:1122832
I have my data set up as follows:
my_data <- data.frame(
learner_code = 1:8,
lsk = c(0, 10, 20, 30, 50, 15, 25, 40)
I want to run the following code to first create categories within the lsk variable based on the learner scores, and then create a table of the counts and proportion of learners in each category.
Here is the current code:
lsk_cat1 <- my_data %>%
mutate(lsk = case_when (lsk == 0 ~ "0",
lsk >0 & lsk < 20 ~ "1-19",
lsk > 19 & lsk < 40 ~ "20-39",
lsk>= 40 ~ "40+" )) %>%
group_by(lsk) %>% summarise(n = length(lsk),
proportion = round(length(lsk)/8*100, 1))
This gives me the following output:
n proportion
8 100
When it should give something like this:
lsk n proportion
1 0 1 12.5
2 1-19 3 37.5
3 20-39 3 37.5
4 40+ 1 12.5
Worked perfectly for another dataset now it's being difficult, would appreciate the help.
I have my data set up as follows:
my_data <- data.frame(
learner_code = 1:8,
lsk = c(0, 10, 20, 30, 50, 15, 25, 40)
I want to run the following code to first create categories within the lsk variable based on the learner scores, and then create a table of the counts and proportion of learners in each category.
Here is the current code:
lsk_cat1 <- my_data %>%
mutate(lsk = case_when (lsk == 0 ~ "0",
lsk >0 & lsk < 20 ~ "1-19",
lsk > 19 & lsk < 40 ~ "20-39",
lsk>= 40 ~ "40+" )) %>%
group_by(lsk) %>% summarise(n = length(lsk),
proportion = round(length(lsk)/8*100, 1))
This gives me the following output:
n proportion
8 100
When it should give something like this:
lsk n proportion
1 0 1 12.5
2 1-19 3 37.5
3 20-39 3 37.5
4 40+ 1 12.5
Worked perfectly for another dataset now it's being difficult, would appreciate the help.
Share Improve this question asked Nov 22, 2024 at 15:32 Paige CoxPaige Cox 556 bronze badges 01 Answer
Reset to default 1We should use cut()
from base.
- Base R
my_data$lsk |>
cut(breaks=c(0, 1, 20, 40, Inf),
labels=c("0", "1-19", "20-39", "40+"),
right=FALSE) |>
table() |>
as.data.frame() |>
transform(i = sprintf("%.1f%%", Freq / sum(Freq) * 100)) |>
setNames(c("lsk", "n", "proportion"))
lsk n proportion
1 0 1 12.5%
2 1-19 2 25.0%
3 20-39 3 37.5%
4 40+ 2 25.0%
- dplyr
library(dplyr)
my_data |>
group_by(lsk = cut(lsk,
breaks=c(0, 1, 20, 40, Inf),
labels=c("0", "1-19", "20-39", "40+"),
right=FALSE)) |>
summarise(n = length(lsk)) |>
mutate(proportion = scales::percent(prop.table(n), .1))
# A tibble: 4 × 3
lsk n proportion
<fct> <int> <chr>
1 0 1 12.5%
2 1-19 2 25.0%
3 20-39 3 37.5%
4 40+ 2 25.0%
Data
Either
my_data = data.frame(learner_code = 1:8, lsk = c(0, 10, 20, 30, 50, 15, 25, 40))
or
my_data = tibble::tibble(learner_code = 1:8, lsk = c(0, 10, 20, 30, 50, 15, 25, 40))
本文标签:
版权声明:本文标题:r - How can I fix this code to create a table of the count and proportion for each level of a variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302695a1931627.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论