1

I have this data set called HAPPY, that has multiple different columns (AGE, MARITAL, DEGREE... and so on). I have these columns to be factor variables, and I am trying to rearrange the levels in the factor so they are in an order that makes sense. For example, I am trying to rearrange the MARITAL factors to c("Never Married" "Married "Separated", "Divorced", "Widowed"). I am supposed to use Dplyr to do this. Possibly using arrange, summarize, and group_by. This is what I did, but this is not close at all and I am struggling to figure this out:

HAPPY %>% group_by(MARITAL) %>% summarize(n = n(), solo = mean(AGE)) %>% arrange(desc(solo))

Thanks for your time!

https://xdaiisu.github.io/ds202materials/hwlabs/HAPPY.rds here is the link to the data

Sam Cole
  • 57
  • 6

1 Answers1

0

After reading your rds file, I noticed that the MARITAL column is in character, not factor. The way to convert it to factor is as follows with the mutate function. After that, you can re-arrange the row order using arrange with desc to make it in descending order.

library(dplyr)

HAPPY2 <- HAPPY %>%
  mutate(MARITAL = factor(MARITAL, 
                          levels = c("NEVER MARRIED", "MARRIED", "SEPARATED", "DIVORCED", "WIDOWED"))) %>%
  arrange(desc(MARITAL))

I am unclear why you want to use summarize and group_by. If you have a new question, please clarify.

www
  • 38,575
  • 12
  • 48
  • 84