如何在 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"
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP