How to group factor levels in R

I'm undertaking a data analysis of the 2017 General Election using the BES Survey. I need to test whether people who identify as religious are more likely to vote for a certain party, but the survey question lists each religion seperately?

How do I combine these answers together to have one label for all religions?

So far I have the following which gives me a breakdown of each party, voting for Conservative and Other but I need it to be only two categories, 'No Religion' and 'All Religions'.

bes17f <- read_spss("bes_f2f_2017_v1.5.sav")
bes17f <- as_factor(bes17f) %>% droplevels()
descriptives(bes17f, vars=c(b02, y06), freq = TRUE, bar = TRUE)
bes17f <- bes17f %>%
  mutate(
    vote17 = recode(b02, 
                    "Don`t know"=NA_character_,
                    "Don't know"=NA_character_,
                    "Refused"=NA_character_,
                    "Not stated" = NA_character_,
                    "Other" = NA_character_,
                    "NA" = NA_character_,
    ),
    Religion17 = recode(y06, 
                       "Don`t know"=NA_character_,
                       "Don't know"=NA_character_,
                       "No religion"=NA_character_,
                       "Refusal" = NA_character_,
    )
  )


bes17f <- bes17f %>%
mutate(vote17_simple = fct_other(vote17, keep=c("Conservatives")))
bes17f %>%
  drop_na(Religion17) %>%
  drop_na(vote17_simple) %>%
  ggplot(aes(x=Religion17)) +
  geom_bar(position="fill", aes(fill=vote17_simple)) +
  theme(axis.text.x = element_text(angle = 20, hjust = 1)) +
  scale_y_continuous(labels = percent)

Thanks in advance!

I think you want mutate(case_when from dplyr, as in this link
case_when

A good example is

starwars %>%
select(name:mass, gender, species) %>% 
mutate( type = case_when( 
     height > 200 | mass > 200 ~ "large", 
     species == "Droid" ~ "robot", 
     TRUE ~ "other" ) )

in your use case, it would be something like:

bes17f %>%
  mutate(case_when(
  vote17 %in% c("Religion1", "Religion2", "Religion3") ~ "All Religions",
  TRUE ~ "No Religion"))
2 Likes

I've formatted your code to make it easier for others to read. Hope you don't mind.

Since you're new here, it might be helpful to know how to properly format code and console output that you post here. Using proper code formatting makes the site easier to read, prevents confusion (unformatted code can get garbled by the forum software :anguished:), and is generally considered the polite thing to do. Check out this FAQ to find out how — it's as easy as the click of a button! :grinning::

Hi: Since we don't have access to your table (or the command descriptives() in my case), it's hard to know what specific suggestion to make. Could you provide a small 'toy' example table that mimics the problem you're trying to solve? Or maybe provide a url to the data and the name of package that contains the command descriptives()?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.