Search code examples
rdplyrdatatabletidyverseforcats

Change the levels of multiple factors that start_with the same pattern in R


I have a long data frame with thousands of observations, but for demonstration purposes I am presenting this

df <- data.frame(col1=rep(c("A","B","C"),3),
                 col2=c("10.01","10.02","10.03","100.1","100.2","100.3","12.1","12.2","12.3"))

 col1  col2
1    A 10.01
2    B 10.02
3    C 10.03
4    A 100.1
5    B 100.2
6    C 100.3
7    A  12.1
8    B  12.2
9    C  12.3

df <- df %>% 
  mutate_all(., as.factor)

levels(df$col2)
"10.01" "10.02" "10.03" "100.1" "100.2" "100.3" "12.1"  "12.2"  "12.3" 

I want to change the order of the levels in the col2 and be like this

"10.01" "10.02" "10.03" "12.1"  "12.2"  "12.3" "100.1" "100.2" "100.3" 

Any help or comment are highly appreciated


Solution

  • Use forcats::fct_inseq:

    df <- df %>% 
      mutate_all(., as.factor) %>% 
      mutate(col2 = fct_inseq(df$col2))
    

    Output:

    levels(df$col2)
    [1] "10.01" "10.02" "10.03" "12.1"  "12.2"  "12.3"  "100.1" "100.2" "100.3"