Reputation: 35
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.
Upvotes: 3
Views: 1841
Reputation: 163217
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"
Upvotes: 1
Reputation: 388862
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)
Upvotes: 5
Reputation: 2363
You can match everything before first space using lookarounds
/^[^\s]+(?=\s)\s+/gm
Upvotes: 0