📜  R 中的 strsplit()函数

📅  最后修改于: 2022-05-13 01:54:36.935000             🧑  作者: Mango

R 中的 strsplit()函数

R 编程语言中的strsplit() 函数用于根据给定的子串作为其参数将指定字符向量的元素拆分为子串。

示例 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, "")

输出:

示例 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"