I would like to order the variables in the below tibble so I have a1, a2, ...d2. How can I achieve that?
library(tidyverse)
dat1 <- tibble(
b1 = c("Math", "Kisw", "Computer", "Biology"),
a1 = c("Science", "Geog", "Studies", "Math"),
a2 = c("Yes", "No", "Yes", "No"),
b2 = rnorm(4, 80, 5),
c2 = rnorm(4, 67, 5),
b4 = rnorm(4, 98, 5),
d2 = rnorm(4, 23, 5),
d1 = rnorm(4, 76, 5),
a5 = rnorm(4, 87, 5)
)
CodePudding user response:
It's not tidyverse, but
dat1[sort(names(dat1))]
will work.
If you have more than 10 sequentially numbered variable names (e.g. a1
up to a20
) you might want to use gtools::mixedsort()
instead of sort()
so that they get sorted a1
, ... a10
, a11
, ..., a20
rather than lexicographically (a1
, a10
, a11
, ... , a2
, a20
, a3
, ...) (gtools::mixedsort
will also work with the select()
and relocate()
-based answers posted by others.)
CodePudding user response:
How about this:
dat1 %>% select(sort(colnames(.)))
# A tibble: 4 × 9
# a1 a2 a5 b1 b2 b4 c2 d1 d2
# <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Science Yes 93.3 Math 72.9 102. 67.1 83.7 24.0
#2 Geog No 90.9 Kisw 77.4 106. 67.2 70.2 23.3
#3 Studies Yes 94.7 Comput… 79.3 109. 67.5 82.1 24.5
#4 Math No 85.0 Biology 75.5 97.4 69.3 83.5 28.5
CodePudding user response:
You don't have to list out all the variables with relocate
:
library(dplyr)
dat1 %>%
relocate(sort(names(.)))
# A tibble: 4 × 9
# a1 a2 a5 b1 b2 b4 c2 d1 d2
# <chr> <chr> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Science Yes 93.3 Math 72.9 102. 67.1 83.7 24.0
#2 Geog No 90.9 Kisw 77.4 106. 67.2 70.2 23.3
#3 Studies Yes 94.7 Comput… 79.3 109. 67.5 82.1 24.5
#4 Math No 85.0 Biology 75.5 97.4 69.3 83.5 28.5
If you want a11 to be after a9, you can use str_sort
instead of sort
:
library(stringr)
library(dplyr)
dat1 %>%
relocate(str_sort(names(.), numeric = TRUE))
# A tibble: 4 × 9
# a2 a5 a11 b1 b2 b4 c2 d1 d2
# <chr> <dbl> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#1 Yes 92.5 Science Math 81.7 100. 67.1 72.8 27.9
#2 No 88.0 Geog Kisw 70.2 95.4 66.0 66.5 26.5
#3 Yes 97.2 Studies Computer 81.3 98.8 64.3 72.4 27.9
#4 No 78.6 Math Biology 76.6 105. 62.9 68.3 23.6