Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort a list alphabetically (R studio)

I’m doing a simple task and something didn’t work. It is necessary to create a list of random letters of the alphabet. And then sort it alphabetically. To create such a list, I use the following code:

# Creating a random vector of letters
random_text_data = sample(letters, 10)
random_text_data

# Convert to list
list_text_data = as.list(random_text_data)
list_text_data

In the console I get the following:

> random_text_data
 [1] "h" "m" "q" "b" "z" "i" "y" "f" "d" "e"
> # Convert to list
> list_text_data = as.list(random_text_data)
> list_text_data
[[1]]
[1] "h"

[[2]]
[1] "m"

[[3]]
[1] "q"

[[4]]
[1] "b"

[[5]]
[1] "z"

[[6]]
[1] "i"

[[7]]
[1] "y"

[[8]]
[1] "f"

[[9]]
[1] "d"

[[10]]
[1] "e"

Now I need to sort it alphabetically. I have tried the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# Sort list alphabetically
sort_data = sort(list_text_data)

But get error:

> sort_data = sort(list_text_data)
Error in sort.int(x, na.last = na.last, decreasing = decreasing, ...) : 
'x' must be elementary

How should you sort?

>Solution :

Here’s a method that do not need to unlist(). After making the list with random letters, assign names to the list with the content of the list. Then order the names within the list.

Input

random_text_data = sample(letters, 10)

[1] "c" "v" "m" "g" "h" "l" "d" "i" "u" "y"

Original solution

list_text_data = as.list(random_text_data)

list_text_data <- setNames(list_text_data, list_text_data)

list_text_data[order(names(list_text_data))]

$c
[1] "c"

$d
[1] "d"

$g
[1] "g"

$h
[1] "h"

$i
[1] "i"

$l
[1] "l"

$m
[1] "m"

$u
[1] "u"

$v
[1] "v"

$y
[1] "y"

Updated solution

Inspired by @zx8754’s answer, you don’t actually need to change names of the list, you can use setNames within order, then the output will be an unnamed list.

list_text_data[order(names(setNames(list_text_data, list_text_data)))]

[[1]]
[1] "c"

[[2]]
[1] "d"

[[3]]
[1] "g"

[[4]]
[1] "h"

[[5]]
[1] "i"

[[6]]
[1] "l"

[[7]]
[1] "m"

[[8]]
[1] "u"

[[9]]
[1] "v"

[[10]]
[1] "y"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading