2
votes

I have tried gsub as following to remove everything before the first space but it didn't work.

lagl2$SUSPENSE <- gsub(pattern = "(.*)\\s",replace=" ", lagl2$SUSPENSE) 

example of the row data: 64400/GL WORKERS COMPENSATION

and I want the result to be like that: WORKERS COMPENSATION

This is just an example but I have many observations and one column and need to delete everything before the first space.

I am new to R and to programming but I started loving it.

3

3 Answers

3
votes

You can remove everything before first space using sub as -

sub(".*?\\s", "", "64400/GL WORKERS COMPENSATION")
#[1] "WORKERS COMPENSATION"

To apply to the whole column you can do -

lagl2$SUSPENSE <- sub(".*?\\s", "", lagl2$SUSPENSE)
0
votes

You can match everything before first space using lookarounds

/^[^\s]+(?=\s)\s+/gm

Demo

0
votes

You could also assert the start of the string ^ , and match optional non whitespace chars followed by one or more whitespace chars using \S*\s+ that you want to remove.

sub("^\\S*\\s+", "", "64400/GL WORKERS COMPENSATION")

Output

[1] "WORKERS COMPENSATION"