R 中的 strsplit()函数
R 编程语言中的strsplit() 函数用于根据给定的子串作为其参数将指定字符向量的元素拆分为子串。
Syntax: strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
Parameters: This function accepts some parameters which are illustrated below:
- x: This is the character vector, data file, or a string whose each element is going to be split.
- split: This parameter says to split the specified string X into the required formats.
- fixed: This parameter accepts logical values. If its value is TRUE, it matches and splits exactly, otherwise use regular expressions.
- perl: This parameter accepts logical values.
- useBytes: This parameter accepts logical values. If its value is TRUE, the matching is done byte-by-byte rather than character-by-character, and inputs with marked encodings are not converted.
Return value: This function returns a new split string.
示例 1:使用 strsplit()函数
在这里,我们将在字符串应用此函数。
R
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GFG is a CS Portal.")
# Calling the strsplit() function over
# the above string to split the given input
# into a list of strings or values
strsplit(String, " ")
R
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GFG//is//a//CS//Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input separated by "//"
# into a list of strings or values
strsplit(String, "//")
R
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GeeksforGeeks13is456a246Computer9Science10Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input separated by the regular expression "[0-9]+"
# into a list of strings or values
strsplit(String, "[0-9]+")
R
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GFG is a CS Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input into each characters as string
strsplit(String, "")
R
# Initializing some date values
Date_values <- c("20-08-2021", "12-07-2019",
"02-05-2020")
# Calling the strsplit() function over the above
# specified date values along with the split
# format of "-"
Result <- strsplit(Date_values, split = "-")
# Getting the split date
Result
# Getting the split date in the form of
# matrix of 3 column
matrix(unlist(Result), ncol=3, byrow=T)
输出 :
[[1]]
[1] "GFG" "is" "a" "CS" "Portal."
示例 2:将分隔符与 strsplit()函数一起使用
分隔符只不过是用于分隔数据中的单词或文本的字符或值。在下面的示例中,strsplit()函数与分隔符“//”一起使用,分隔符“//”分隔由分隔符“//”分隔的单词。
电阻
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GFG//is//a//CS//Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input separated by "//"
# into a list of strings or values
strsplit(String, "//")
输出:
[[1]]
[1] "GFG" "is" "a" "CS" "Portal."
示例 3:将正则表达式与 strsplit()函数一起使用
在下面的示例中,文本用正则表达式“[0-9]+”分隔,strsplit()函数用于拆分由该正则表达式分隔的文本。
电阻
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GeeksforGeeks13is456a246Computer9Science10Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input separated by the regular expression "[0-9]+"
# into a list of strings or values
strsplit(String, "[0-9]+")
输出:
[[1]]
[1] "GeeksforGeeks" "is" "a" "Computer"
[5] "Science" "Portal."
示例 4:拆分字符串的每个字符
在下面的示例中,输入字符串的每个字符都在 strsplit()函数的帮助下进行拆分。
电阻
# R program to illustrate
# strsplit() function
# Initializing a string
String <- ("GFG is a CS Portal.")
# Calling the strsplit() function over
# the above string to split the given
# input into each characters as string
strsplit(String, "")
输出:
[[1]]
[1] “G” “F” “G” ” ” “i” “s” ” ” “a” ” ” “C” “S” ” ” “P” “o” “r” “t” “a” “l” “.”
示例 5:使用此函数拆分日期
在下面的示例中,strsplit()函数用于将指定的日期拆分为日期、月份和年份等每个部分。
电阻
# Initializing some date values
Date_values <- c("20-08-2021", "12-07-2019",
"02-05-2020")
# Calling the strsplit() function over the above
# specified date values along with the split
# format of "-"
Result <- strsplit(Date_values, split = "-")
# Getting the split date
Result
# Getting the split date in the form of
# matrix of 3 column
matrix(unlist(Result), ncol=3, byrow=T)
输出:
[[1]]
[1] "20" "08" "2021"
[[2]]
[1] "12" "07" "2019"
[[3]]
[1] "02" "05" "2020"
[,1] [,2] [,3]
[1,] "20" "08" "2021"
[2,] "12" "07" "2019"
[3,] "02" "05" "2020"