This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Reorder "matrix" "array"

Hi there,

Is there any function to reorder "matrix" "array" based on specific criteria?

For example, I got an array with column names A to Z.

I want to reorder columns so that column names with vowels (a, e, i, o, u) appear first in the array and the rest of the letters column later.

Any thoughts

Best: Imran

r

1 answer

assuming your array is:

library(dplyr)

ar <- t(array(data     = 1:27,
            dim      = c(20, 3),
            dimnames = list(c(sample(letters, 20)))))

class(ar)

use dplyr() to transform it in a tibble, move the columns of interest and convert back to array

ar %>% as_tibble() %>% relocate(any_of(c("a", "e", "i", "o", "u"))) %>% simplify2array()

Log in to answer this question.