Convert character values in a vector to numeric values

Viewed 45

I have the following vector, called c_vector:

c_vector <- c("0, 1, 2, 3", "4, 5, 7", "8, 9, 1")

I need to convert c into the following numeric vector

c_vector <- c(0, 1, 2, 3, 4, 5, 7, 8, 9 ,1)

How can I achieve this? Thank you.

2 Answers

We can use scan directly

scan(text = c_vector, what = numeric(), sep=",")
#[1] 0 1 2 3 4 5 7 8 9 1

EDIT: Comments from @A5C1D2H2I1M1N2O1R2T1

Maybe you can try the code below

c_vector<-as.numeric(unlist(strsplit(c_vector,", ")))
Related