我正在寻找一种使用grep方法获取字符串中第一个空格前字符的方式。
由于我无法想出如何在R
中使用grep
类命令来实现它,所以我已经修改了以下函数。
如果有解决方案,请帮忙提供grep
的解决方法...
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"
如果有比 grep
(或我的函数 beforeSpace
)更好的方法,请让我知道。