Supose I have the following
l="1,2,3,4,5"
How do I get the sequence of numeric values
1 2 3 4 5
I've already seen this example https://statisticsglobe.com/convert-character-to-numeric-in-r/ But it doesn't quite match with the problem above.
CodePudding user response:
This is one way of doing this:
library(stringr)
l="1,2,3,4,5"
as.numeric(str_split(l, ',', simplify = TRUE))
CodePudding user response:
1) scan will convert such a string to a numeric vector. Omit the quiet argument if you would like it to report the length of the result. No packages are used.
x <- "1,2,3,4,5"
scan(text = x, sep = ",", quiet = TRUE)
## [1] 1 2 3 4 5
2) If what you have is actually a vector of comma separated character stings. xx. and a list of numeric vectors is wanted then lapply over them.
xx <- c(x, x)
lapply(xx, function(x) scan(text = x, sep = ",", quiet = TRUE))
## [[1]]
## [1] 1 2 3 4 5
##
## [[2]]
## [1] 1 2 3 4 5