I would like to sort (and rearrange) columns in a tibble object. The table (let's call it dataSet
) that I am working with looks like this:
Fall 2019 | Fall 2020 | Spring 2019 | Spring 2020 | Spring 2021 |
---|---|---|---|---|
1 | 36 | 32 | 43 | 25 |
15 | 84 | 94 | 64 | 65 |
I would like to have it sorted in the following order.
Spring 2019 | Fall 2019 | Spring 2020 | Fall 2020 | Spring 2021 |
---|---|---|---|---|
32 | 1 | 43 | 36 | 25 |
94 | 15 | 64 | 84 | 65 |
Since the columns are essentially strings, I have tried using str_sort(names(dataSet))
. However, this returns the original column names as it is already sorted. If I can get the column's name sorted, I know I can rearrange the columns in the tibble object. I've worked with time objects before, but those are usually for formats like mm/dd/yyyy, but nothing in terms of semesters. Any help will be greatly appreciated. Thanks in advance!
CodePudding user response:
This can work for the data provided but the order of seasons can vary depending on what part of the world we are in.
library(tidyverse)
df <- tibble(`Fall 2019` = c(1, 15), `Fall 2020` = c(36, 84), `Spring 2019` = c(32, 94), `Spring 2020` = c(43, 64), `Spring 2021` = c(25, 65))
names(df) %>% str_match('^(\\D ) (\\d )') %>%
as_tibble %>%
arrange(V3, desc(V2)) %>% `[`(, 1) %>%
pull %>%
{select_at(df, .)}
#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
#> Using compatibility `.name_repair`.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was generated.
#> # A tibble: 2 × 5
#> `Spring 2019` `Fall 2019` `Spring 2020` `Fall 2020` `Spring 2021`
#> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 32 1 43 36 25
#> 2 94 15 64 84 65
Created on 2021-12-01 by the reprex package (v2.0.1)
CodePudding user response:
Use relocate
here with the indices. You could also use the names:
library(dplyr)
df %>%
relocate(3,1,4,2,5)
Spring2019 Fall2019 Spring2020 Fall2020 Spring2021
1 32 1 43 36 25
2 94 15 64 84 65