當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R stringr word 從句子中提取單詞

從句子中提取單詞

用法

word(string, start = 1L, end = start, sep = fixed(" "))

參數

string

輸入向量。或者是一個字符向量,或者是可強製轉換為一個的東西。

start, end

給出要提取的單詞範圍(包括)的整數向量對。如果為負數,則從最後一個單詞開始倒數。

默認值選擇第一個單詞。

sep

單詞之間的分隔符。默認為單個空格。

string /start /end 長度相同的字符向量。

例子

sentences <- c("Jane saw a cat", "Jane sat down")
word(sentences, 1)
#> [1] "Jane" "Jane"
word(sentences, 2)
#> [1] "saw" "sat"
word(sentences, -1)
#> [1] "cat"  "down"
word(sentences, 2, -1)
#> [1] "saw a cat" "sat down" 

# Also vectorised over start and end
word(sentences[1], 1:3, -1)
#> [1] "Jane saw a cat" "saw a cat"      "a cat"         
word(sentences[1], 1, 1:4)
#> [1] "Jane"           "Jane saw"       "Jane saw a"     "Jane saw a cat"

# Can define words by other separators
str <- 'abc.def..123.4568.999'
word(str, 1, sep = fixed('..'))
#> [1] "abc.def"
word(str, 2, sep = fixed('..'))
#> [1] "123.4568.999"
源代碼:R/word.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Extract words from a sentence。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。