Kumpelka
Kumpelka

Reputation: 987

R - Change the order of elements in a list

I need to change the order of elements in a list. I don't find a patent response in others questions about sort or order a list.

Here is a simple example.

Freedom <- c(1, 2, 3, 2, 1, 2)
Equality <- c(2, 3, 1, 1, 2, 1)
TypeCountry <- c("South", "East", "East", "North", "South", "West")

Example <- list(Freedom, Equality, TypeCountry)
names(Example) <- c("Freedom", "Equality", "TypeCountry")

The list has the order Freedom, Equality then TypeCountry and I want to be able to change the order of the elements (for example Equality, Freedom then TypeCountry).

Upvotes: 14

Views: 19376

Answers (2)

Diego Rodrigues
Diego Rodrigues

Reputation: 864

Just do this:

Ex <- Example[c("TypeCountry","Freedom", "Equality")]

You specify the order you want.

Upvotes: 21

akrun
akrun

Reputation: 887981

We can order on the names of 'Example'

ExampleNew <- Example[order(names(Example))]

Upvotes: 10

Related Questions