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

Extract unique numbers from a list with multiple items per line using gsub()?

List A1 structured like this:

#[[1]]
[1] "12" "1"

#[[2]]
[1] "13" "1"

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

#[[3]]
[1] "14" "2"

#[[4]]
[1] "15" "2"

#[[5]]
[1] "16" "1"

#[[6]]
[1] "18" "2"

#[[7]]
[1] "20" "0"

#[[8]]
[1] "21" "2"

#[[9]]
[1] "21" "4"

#[[10]]
[1] "34" "1" "0"

#[[11]]
[1] "42" "1" "1"

#[[12]]
[1] "42" "2" "2"

and I’m trying to extract the unique values in the first item of each list element (for example, the number 12 in [[1]]).

When I use
v <- unique(gsub( " .*$", "", A1))

the resulting vector v looks like

"c("12"," "c("13"," "c("14"," "c("15"," "c("16"," "c("18"," "c("20"," "c("21","
"c("34"," "c("42","

The desired result would be (12, 13, 14, 15, 16, 17, 18, 20, 21, 34, 42)

How do I get rid of the additional characters, where do they come from?

Thank you in advance!

>Solution :

You can use

v <- list(c("12", "1"), c("13", "1"), c("12", "3"))
unique(sapply(v, "[[", 1))
# => [1] "12" "13"

See the R demo online.

Note:

  • sapply(v, "[[", 1) – gets the first items
  • unique leaves only the unique values.
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