Reputation: 65
I am for sure no expert in R yet. I grew up with SPSS and is slowly shifting to R. I solve problems as I meet them. And seek help when I get lost.
Please look at this code:
dataset$v18[dataset$s_18 == 1] <- "Agree"
dataset$v18[dataset$s_18 == 2] <- "Partly Agree"
dataset$v18[dataset$s_18 == 3] <- "Neutral"
dataset$v18[dataset$s_18 == 4] <- "Partly disagree"
dataset$v18[dataset$s_18 == 5] <- "Disagree"
sv18x <- dataset %>%
filter(!is.na(v18)) %>%
group_by(v18) %>%
dplyr::summarise(count=n()) %>%
mutate(pct=count/sum(count)*100)
sv18x$v18 <- factor(sv18x$v18,levels = c("Agree", "Partly agree", "Neutral", "Partly disagree", "Disagree uenig"))
sv18x$pct<- trunc(sv18x$pct)
I feel quite confident what this can be done in a shorter and smarter way. And I think it should be done using dplyr::recode() and something else that I probably don't know yet. I just can't figure out how to do it. Can someone give me a hint?
Upvotes: 0
Views: 87
Reputation: 348
I simulated a reproducible example to help you, but it's hard to know what you want without the real dataset. The first part can be done with dplyr::case_when(), while the percentage part can be done with the janitor package.
library(dplyr)
library(janitor)
dataset <- data.frame(ola = sample(c("a", "b", "c", 150, replace = TRUE)),
s_18 = sample(1:5, 150, replace = TRUE))
dataset <- dataset %>%
mutate(v18 = case_when(
s_18 == 1 ~ "Agree",
s_18 == 2 ~ "Partly Agree",
s_18 == 3 ~ "Neutral",
s_18 == 4 ~ "Partly Disagree",
s_18 == 5 ~ "Disagree"
))
sv18x <- dataset %>%
count(v18) %>%
janitor::adorn_percentages("col") %>%
janitor::adorn_pct_formatting()
Hope this helps!
Upvotes: 1