1) strapply
1a) scalar Here is a one-liner using strapply from the gsubfn package:
library(gsubfn)
x <- '"12,34,567"'
strapply(x, "\\d+", as.numeric, simplify = c)
## [1] 12 34 567
1b) vectorized A vectorized version is even simpler -- just remove the simplify=c like this:
v <- c('"1,2,3"', '"8,9"') # test data
strapply(v, "\\d+", as.numeric)`
2) gsub and scan
2a) scalar and here is a one-linear using gsub and scan:
scan(text = gsub('"', '', x), what = 0, sep = ",")
## Read 3 items
## [1] 12 34 567
2b) vectorized A vectorized version would involve lapply-ing over the components:
lapply(v, function(x) scan(text = gsub('"', '', x), what = 0, sep = ","))
3) strsplit
3a) scalar and here is a strsplit solution. Note that we split on both " and , :
as.numeric(strsplit(x, '[",]')[[1]][-1])
## [1] 12 34 567
3b) vectorized A vectorized solution would, again, involve lapply-ing over the components:
lapply(v, function(x) as.numeric(strsplit(x, '[",]')[[1]][-1]))
3c) vectorized - simpler or slightly simpler:
lapply(strsplit(gsub('"', '', v), split = ","), as.numeric)
gsub-bing orregex-ing. - daedalus