Usman Khaliq
Usman Khaliq

Reputation: 363

Convert character values in a vector to numeric values

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.

Upvotes: 2

Views: 56

Answers (2)

akrun
akrun

Reputation: 887981

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

Upvotes: 2

ThomasIsCoding
ThomasIsCoding

Reputation: 102920

Maybe you can try the code below

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

Upvotes: 2

Related Questions