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

Convert a list of number groups (in character format) to a list of numeric values

I have a list that looks like this.
Its character format, but would like these separated into lists of numbers.

mylist <- c("8,9,10", "5,6,7,3,4,5,7", "9,10,6,8,9")
> mylist
[1] "8,9,10"        "5,6,7,3,4,5,7" "9,10,6,8,9"   

This is how I’d like to convert it

list_result <- list(c(8,9,10), c(5,6,7,3,4,5,7),c (9,10,6,8,9) )
> list_result
[[1]]
[1]  8  9 10

[[2]]
[1] 5 6 7 3 4 5 7

[[3]]
[1]  9 10  6  8  9

Is there any simple function that can do this? Any help greatly appreciated. Thanks

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

>Solution :

lapply(strsplit(mylist, ","), as.numeric)

or

type.convert(strsplit(mylist, ","), as.is = TRUE)
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