From character vector or data frame how can I separate numbers and strings

a<-c(1,2,3,4,5,"a","b","c","sadasc")

Is there an alternative solution for this

b<-as.numeric(a[1:5])  # Not using the address of the values

How to separate numbers from a vector or dataframe w/o using above method.

You can use regular expresions (regex)

a<-c(1,2,3,4,5,"a","b","c","sadasc")
b <- as.numeric(grep("[0-9]",a,value = TRUE))
b
#> [1] 1 2 3 4 5

Created on 2019-01-17 by the reprex package (v0.2.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.