如何在 R 中從字串中提取第一個、最後一個或中間字元?


在文字分析中,我們可能需要從一個字串中提取字元,或者從向量的字串中提取字元。此提取可能需要建立一個字串,其中包含用於進一步分析的某些特定單詞。藉助 stringr 包的 str_sub 函式,我們可以做到這一點。

示例

考慮以下字串 -

> x1<-"Removing harmful things from the road is an act of charity"

載入 stringr 包 -

> library(stringr)
> str_sub(x1,1,8)
[1] "Removing"
> str_sub(x1,1,23)
[1] "Removing harmful things"
> str_sub(x1,29,37)
[1] " the road"
> str_sub(x1,30,37)
[1] "the road"
> str_sub(x1,-58,-51)
[1] "Removing"
> str_sub(x1,-58,-1)
[1] "Removing harmful things from the road is an act of charity"
> str_sub(x1,-7,-1)
[1] "charity"
> str_sub(x1,-14,-1)
[1] "act of charity"
> str_sub(x1,-17,-1)
[1] "an act of charity"

讓我們來看看對向量的字串數的提取 -

> x1<-c("Removing", "harmful", "things", "from", "the", "road", "is", "an", "act", "of", "charity")
> str_sub(x1,1,2)
[1] "Re"  "ha"  "th"  "fr"  "th"  "ro"  "is"  "an"  "ac"  "of"  "ch"
> str_sub(x1,1,3)
[1] "Rem" "har" "thi" "fro" "the" "roa" "is" "an" "act" "of" "cha"
> str_sub(x1,1,10)
[1] "Removing" "harmful" "things" "from" "the" "road"
[7] "is"  "an"   "act"    "of"    "charity"
> str_sub(x1,-7,-2)
[1] "emovin" "harmfu" "thing" "fro" "th" "roa" "i" "a"
[9] "ac" "o" "charit"
> str_sub(x1,-7,-1)
[1] "emoving" "harmful" "things" "from" "the" "road" "is"
[8] "an" "act" "of"     "charity"
> str_sub(x1,-10,-1)
[1] "Removing" "harmful" "things" "from" "the" "road"
[7] "is" "an" "act" "of" "charity"

更新於: 11-Aug-2020

1K+ 檢視次數

開啟您的 職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.