In the answers below, we use this test data:
# test data v1 <- c("Pic 26 + 25", "Pic 27 + 28", "Pic 28 + 27", "Pic 29 + 30", "Pic 30 + 29", "Pic 31 + 32")
1) gsubfn
library(gsubfn) strapply(v1, "(\\d+).*", as.numeric, simplify = c)
2) sub This does not require packages, but includes a slightly longer regular expression:
as.numeric( sub("\\D*(\\d+).*", "\\1", v1) )
3) read.table This does not contain regular expressions or packages:
read.table(text = v1, fill = TRUE)[[2]] ## [1] 26 27 28 29 30 31
In this particular example, fill=TRUE may be omitted, but may be required if the v1 components had a different number of fields.
G. grothendieck
source share