简体   繁体   中英

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 ).

Just do this:

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

You specify the order you want.

我们可以order “示例”的names

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM