I am looking for a grep way to obtain the characters in a string prior to the first space.
I have hacked the following function, as i could not figure out how to do it using the grep
type commands in R
.
Could someone help with the grep
solution - if there is one ...
beforeSpace <- function(inWords) {
vapply(inWords, function(L) strsplit(L, "[[:space:]]")[[1]][1], FUN.VALUE = 'character')
}
words <- c("the quick", "brown dogs were", "lazier than quick foxes")
beforeSpace(words)
R> the quick brown dogs were lazier than quick foxes
"the" "brown" "lazier"
And do let me know if there's a better way than grep
(or my function, beforeSpace
) to do this.