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

>Solution :

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

or

type.convert(strsplit(mylist, ","), as.is = TRUE)

Leave a Reply