Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a string and white space in R

I have a dataframe with columns having values like:

"Average 18.24" "Error 23.34". My objective is to replace the text and following space from these. in R. Can any body help me with a regex pattern to do this?

I am able to successfully do this using the [A-Z]. But i am not able to combine the white space. [A-Z][[:space:]] no luck. Your help is appreciated.

like image 796
duvvurum Avatar asked Jul 15 '16 09:07

duvvurum


2 Answers

We can use sub. Use the pattern \\D+ to match all non-numeric characters and then use '' in the replacement to remove those.

sub("\\D+", '', v2)
#[1] "18.24" "23.34"

Or match one or more word characters followed by one or more space and replace with ''.

 sub("\\w+\\s+", "", v2)
 #[1] "18.24" "23.34"

Or if we are using stringr

library(stringr)
word(v2, 2)
#[1] "18.24" "23.34"

data

v2 <- c("Average 18.24" ,"Error 23.34")
like image 109
akrun Avatar answered Sep 19 '22 15:09

akrun


You can use a quantifier and add a-z to the pattern (and the ^ anchor)

You can use

"^\\S+\\s+"
"^[a-zA-Z]+[[:space:]]+"

See regex demo

R demo:

> b <- c("Average 18.24", "Error 23.34")
> sub("^[A-Za-z]+[[:space:]]+", "", b)
> ## or sub("^\\S+\\s+", "", b)
[1] "18.24" "23.34"

Details:

  • ^ - start of string
  • [A-Za-z]+ - one or more letters (replace with \\S+ to match 1 or more non-whitespaces)
  • [[:space:]]+ - 1+ whitespaces (or \\s+ will match 1 or more whitespaces)
like image 37
Wiktor Stribiżew Avatar answered Sep 17 '22 15:09

Wiktor Stribiżew