当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


R stringr str_split 将字符串分成几段


这些函数的主要区别在于其输入和输出类型:

  • str_split() 接受一个字符向量并返回一个列表。

  • str_split_1() 接受单个字符串并返回字符向量。

  • str_split_fixed() 接受一个字符向量并返回一个矩阵。

  • str_split_i() 接受一个字符向量并返回一个字符向量。

用法

str_split(string, pattern, n = Inf, simplify = FALSE)

str_split_1(string, pattern)

str_split_fixed(string, pattern, n)

str_split_i(string, pattern, i)

参数

string

输入向量。或者是一个字符向量,或者是可强制转换为一个的东西。

pattern

要寻找的模式。

默认解释是正则表达式,如 vignette("regular-expressions") 中所述。使用regex() 可以更好地控制匹配行为。

使用 fixed() 匹配固定字符串(即仅比较字节)。这很快,但是是近似值。一般来说,为了匹配人类文本,您需要coll(),它尊重指定区域设置的字符匹配规则。

将字符、单词、行和句子边界与 boundary() 匹配。空模式“”相当于 boundary("character")

n

返回的最大件数。默认 (Inf) 使用所有可能的分割位置。

对于 split_split() ,这决定了输出的每个元素的最大长度。对于 str_split_fixed() ,这决定了输出中的列数;如果输入太短,结果将用 "" 填充。

simplify

一个布尔值。

  • FALSE(默认值):返回字符向量列表。

  • TRUE :返回字符矩阵。

i

要返回的元素。使用负值从右侧开始计数。

  • str_split_1():字符向量。

  • str_split() :与 string /pattern 长度相同的列表,包含字符向量。

  • str_split_fixed() :具有 n 列和与 string /pattern 长度相同的行数的字符矩阵。

  • str_split_i() :与 string /pattern 长度相同的字符向量。

也可以看看

stri_split() 用于底层实现。

例子

fruits <- c(
  "apples and oranges and pears and bananas",
  "pineapples and mangos and guavas"
)

str_split(fruits, " and ")
#> [[1]]
#> [1] "apples"  "oranges" "pears"   "bananas"
#> 
#> [[2]]
#> [1] "pineapples" "mangos"     "guavas"    
#> 
str_split(fruits, " and ", simplify = TRUE)
#>      [,1]         [,2]      [,3]     [,4]     
#> [1,] "apples"     "oranges" "pears"  "bananas"
#> [2,] "pineapples" "mangos"  "guavas" ""       

# If you want to split a single string, use `str_split1`
str_split_1(fruits[[1]], " and ")
#> [1] "apples"  "oranges" "pears"   "bananas"

# Specify n to restrict the number of possible matches
str_split(fruits, " and ", n = 3)
#> [[1]]
#> [1] "apples"            "oranges"           "pears and bananas"
#> 
#> [[2]]
#> [1] "pineapples" "mangos"     "guavas"    
#> 
str_split(fruits, " and ", n = 2)
#> [[1]]
#> [1] "apples"                        "oranges and pears and bananas"
#> 
#> [[2]]
#> [1] "pineapples"        "mangos and guavas"
#> 
# If n greater than number of pieces, no padding occurs
str_split(fruits, " and ", n = 5)
#> [[1]]
#> [1] "apples"  "oranges" "pears"   "bananas"
#> 
#> [[2]]
#> [1] "pineapples" "mangos"     "guavas"    
#> 

# Use fixed to return a character matrix
str_split_fixed(fruits, " and ", 3)
#>      [,1]         [,2]      [,3]               
#> [1,] "apples"     "oranges" "pears and bananas"
#> [2,] "pineapples" "mangos"  "guavas"           
str_split_fixed(fruits, " and ", 4)
#>      [,1]         [,2]      [,3]     [,4]     
#> [1,] "apples"     "oranges" "pears"  "bananas"
#> [2,] "pineapples" "mangos"  "guavas" ""       

# str_split_i extracts only a single piece from a string
str_split_i(fruits, " and ", 1)
#> [1] "apples"     "pineapples"
str_split_i(fruits, " and ", 4)
#> [1] "bananas" NA       
# use a negative number to select from the end
str_split_i(fruits, " and ", -1)
#> [1] "bananas" "guavas" 
源代码:R/split.R

相关用法


注:本文由纯净天空筛选整理自Hadley Wickham等大神的英文原创作品 Split up a string into pieces。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。