James Martherus
James Martherus

Reputation: 1043

Reorder factor levels by pattern

I have a factor with that identifies strata within a survey dataset. I want to reorder the factor such that certain character patterns come before other character patterns.

For example, I have this mixed up factor which indicates gender, age, and education:

my_factor <- factor(levels=c(1:8),
                    labels=c("Male-18_34-HS","Female-35_49-HS",
                             "Male-18_34-CG", "Female-18_34-CG",
                             "Male-35_49-HS", "Male-35_49-CG",
                             "Female-18_34-HS", "Female-35_49-CG"),
                    ordered=TRUE)

I'd like this to be ordered with all Female categories first, then the age categories in the correct order, then the education categories in the correct order. I can get most of the way there with forcats::fct_relevel:

forcats::fct_relevel(my_factor, sort)

ordered(0)
8 Levels: Female-18_34-CG < Female-18_34-HS < Female-35_49-CG < Female-35_49-HS < Male-18_34-CG < Male-18_34-HS < ... < Male-35_49-HS

But the education categories are in the wrong order. Is there a way to make sure that "HS" comes before "CG" but leave the order of gender and age groups the same?

Upvotes: 2

Views: 839

Answers (4)

Waldi
Waldi

Reputation: 41260

You could use str_split to split the labels, order the generated list, and rebuild the levels accordingly:

lvl <- do.call(rbind,stringr::str_split(levels(my_factor),'-'))
lvl <- apply(lvl[order(lvl[,1],lvl[,2],lvl[,3]),],1,paste0,collapse='-')
my_factor <- factor(my_factor,levels = lvl)

levels(my_factor)
#> [1] "Female-18_34-CG" "Female-18_34-HS" "Female-35_49-CG" "Female-35_49-HS"
#> [5] "Male-18_34-CG"   "Male-18_34-HS"   "Male-35_49-CG"   "Male-35_49-HS"

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 389235

You can create your desired factor levels programmatically.

lvls <- do.call(paste, c(tidyr::expand_grid(
           c('Female', 'Male'), c('18_34', '35_49'), c('HS', 'CG')), sep = '-'))
lvls
#[1] "Female-18_34-HS" "Female-18_34-CG" "Female-35_49-HS" "Female-35_49-CG"
#[5] "Male-18_34-HS"   "Male-18_34-CG"   "Male-35_49-HS"   "Male-35_49-CG"

You can use this lvls as levels in the factor call.

Upvotes: 2

Paul
Paul

Reputation: 2959

You can make a reference table, arranging by column factor levels:

library(dplyr)
library(tidyr)

ref <- tibble(key = c("Male-18_34-HS","Female-35_49-HS",
                      "Male-18_34-CG", "Female-18_34-CG",
                      "Male-35_49-HS", "Male-35_49-CG",
                      "Female-18_34-HS", "Female-35_49-CG"))

ref <- separate(ref, key, into = c("gender", "age", "education"), sep = "-", remove = FALSE) %>%
  mutate(across("gender", factor, c("Female", "Male")),
         across("age", factor, c("18_34", "35_49")),
         across("education", factor, c("HS", "CG"))) %>%
  arrange(gender, age, education)

Then apply with:

factor(d, levels = ref$key)

Upvotes: 2

Ashish Baid
Ashish Baid

Reputation: 533

dft<-c("Male-18_34-HS","Female-35_49-HS", "Male-18_34-CG", "Female-18_34-CG", "Male-35_49-HS", "Male-35_49-CG", "Female-18_34-HS", "Female-35_49-CG")

gender<-unlist(lapply(dft, FUN=function(x) str_split(x,'-')[[1]][1]))
age<-unlist(lapply(dft, FUN=function(x) str_split(x,'-')[[1]][1]))
ed<-unlist(lapply(dft, FUN=function(x) str_split(x,'-')[[1]][3]))

order_f<-order(gender,age,sort(ed,decreasing = T))

my_factor <- factor(levels=c(1:8),
                    labels=dft[order_f],
                    ordered=TRUE)

Upvotes: 1

Related Questions