Use gsub remove all string before first white space in R

Try this:

sub(".*? ", "", D$name)

Edit:

The pattern is looking for any character zero or more times (.*) up until the first space, and then capturing the one or more characters ((.+)) after that first space. The ? after .* makes it “lazy” rather than “greedy” and is what makes it stop at the first space found. So, the .*? matches everything before the first space, the space matches the first space found.

Leave a Comment