How to split a decimal number from a string in R

For example:

c("2.11abc","15.1cde",".562342rfg") 

How to break a number from this vector? The vector that I would like to have is

 c(2.11, 15.1, 0.562342) 

I tried

 gsub("[^[:digit:]]", "", a) 

but the result was

 c(211, 151 ,562342) 

I really appreciate your help.

+8
r
source share
4 answers

If you have more unpleasant lines in which periods may appear outside the line part number, you can use something like this.

 library(gsubfn) ## A possibly more realistic character vector x <- c("2.11abc.def","ab-15.1cde",".562342rfg", "abcdef") getNumberPart <- function(x) { pat <- "(-?(\\d*\\.*\\d+|\\d+\\.))" strapply(x, pattern=pat, FUN=as.numeric, simplify=TRUE, empty=NA) } getNumberPart(x) # [1] 2.110000 -15.100000 0.562342 NA 

Note that if a string contains more than one number, strapply() will extract all of them and return the overall result as a list (rather than a simple vector) with one list item on the input string.

+7
source share

You are missing a period:

 as.numeric(gsub("[^[:digit:].]", "", c("2.11abc","15.1cde",".562342rfg"))) # [1] 2.110000 15.100000 0.562342 
+5
source share

For ease of use, I would suggest looking at the "qdapRegex" package with which you can do:

 x <- c("2.11abc.def","a.b15.1cde","-.562342rfg", "abcdef") library(qdapRegex) rm_number(x, extract = TRUE) # [[1]] # [1] "2.11" # # [[2]] # [1] "15.1" # # [[3]] # [1] "-.562342" # # [[4]] # [1] NA 
+1
source share

This should help.

 a <- c("2.11abc","15.1cde",".562342rfg") substr(a, 1, regexpr("[a-zA-Z]", a)-1) 
0
source share

All Articles