R语言 从字符向量中提取子串 – substring() 函数

您所在的位置:网站首页 从字符串中提取数字的函数 R语言 从字符向量中提取子串 – substring() 函数

R语言 从字符向量中提取子串 – substring() 函数

2024-06-11 03:05| 来源: 网络整理| 查看: 265

R语言 从字符向量中提取子串 – substring() 函数

R编程语言中的 substring() 函数是用来提取字符向量中的子串的。你可以轻松地从给定的字符串中提取所需的子串或字符。

语法: substring(text, first, last)

参数

text: 字符向量 first: 整数,要被替换的第一个元素 last: 整数,要替换的最后一个元素。 R – substring() 函数示例 例1:用R编程语言中的substring函数提取数值 # R program to illustrate # substring function   # Calling substring() function substring("Geeks", 2, 3) substring("Geeks", 1, 4) substring("GFG", 1, 1) substring("gfg", 3, 3)

输出:

[1] "ee" [1] "Geek" [1] "G" [1] "g" 例2:在R编程语言的字符串函数中用子串提取数值 # R program to illustrate # substring function   # Initializing a string vector x < - c("GFG", "gfg", "Geeks")   # Calling substring() function substring(x, 2, 3) substring(x, 1, 3) substring(x, 2, 2)

输出

[1] "FG" "fg" "ee" [1] "GFG" "gfg" "Gee" [1] "F" "f" "e" 例3:使用substring()函数在R中替换字符串 # R program to illustrate # substring function   # Initializing a string vector x


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3