0

I'm looking forward to extract the numbers of a character string. Unfortunately, I'm getting the left part of it.

Data:
> T3_C2_Z2_Stata_S
     [,1]                  
[1,] "Min.   :-0.0031685  "

R-Code:

T3_C2_Z2_Stata_S<-as.numeric(unlist(strsplit(T3_C2_Z2_Stata_S,split=':',fixed=TRUE))[1])*100

It is not working because the inner function with unllist is returning the following:

> T3_C2_Z2_Stata_S
[1] "Min.   "

My desired output would be:

> T3_C2_Z2_Stata_S
     [,1]                  
[1,] -0.31685

Does anyone know how to get this? Thank you.

Cettt
  • 11,460
  • 7
  • 35
  • 58
Fabricio
  • 35
  • 5

1 Answers1

1

You can try str_extract from the stringr package:

x <- "Min.   :-0.0031685  "
as.numeric(str_extract(x, "-?\\d+\\.?\\d*")) *100
[1] -0.31685

Alternatively, using str_split you have to replace [1] by [2]:

as.numeric(unlist(strsplit(x, split = ':', fixed = TRUE))[2])*100
[1] -0.31685
Cettt
  • 11,460
  • 7
  • 35
  • 58