如何在 R 中從字串向量中提取單詞?


要從字串向量中提取單詞,我們可以使用 stringr 軟體包的單詞函式。例如,如果我們有一個名為 x 的向量,其中包含 100 個單詞,那麼可以使用該命令提取前 20 個單詞:word(x,start=1,end=20,sep=fixed(" ")))。如果我們想從其他單詞開始,那麼起始值將相應改變。

示例

 線上演示

x<-c("R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell Labs Language S.")
x

輸出

[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell Labs Language S."

示例

library(stringr)
word(x,start=1,end=5,sep=fixed(" "))

輸出

[1] "R is a programming language"

示例

word(x,start=1,end=20,sep=fixed(" "))

輸出

[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross"

示例

word(x,start=1,end=10,sep=fixed(" "))

輸出

[1] "R is a programming language and software environment for statistical"

示例

word(x,start=1,end=15,sep=fixed(" "))

輸出

[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting."

示例

word(x,start=1,end=50,sep=fixed(" "))

輸出

[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public"

示例

word(x,start=11,end=20,sep=fixed(" "))

輸出

[1] "analysis, graphics representation and reporting. R was created by Ross"

示例

word(x,start=51,end=60,sep=fixed(" "))

輸出

[1] "License, and pre-compiled binary versions are provided for various operating"

示例

word(x,start=6,end=10,sep=fixed(" "))

輸出

[1] "and software environment for statistical"

示例

word(x,start=11,end=60,sep=fixed(" "))

輸出

[1] "analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating"

示例

word(x,start=1,end=90,sep=fixed(" "))

輸出

[1] "R is a programming language and software environment for statistical analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"

示例

word(x,start=11,end=90,sep=fixed(" "))

輸出

[1] "analysis, graphics representation and reporting. R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"

示例

word(x,start=21,end=90,sep=fixed(" "))

輸出

[1] "Ihaka and Robert Gentleman at the University of Auckland, New Zealand, and is currently developed by the R Development Core Team. R is freely available under the GNU General Public License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka),"

示例

word(x,start=51,end=100,sep=fixed(" "))

輸出

[1] "License, and pre-compiled binary versions are provided for various operating systems like Linux, Windows and Mac. This programming language was named R, based on the first letter of first name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a play on the name of the Bell"

更新於: 2021 年 2 月 10 日

3K+ 次瀏覽

開啟你的職業生涯

完成課程獲取認證

開始
廣告