Reorder "matrix" "array"
1
0
Entering edit mode
3.7 years ago

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 • 766 views
ADD COMMENT
3
Entering edit mode
3.7 years ago
lessismore ★ 1.4k

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()
ADD COMMENT
1
Entering edit mode

Thank you so much. This is what I was looking for.

ADD REPLY

Login before adding your answer.

Traffic: 1966 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6