R 语言学习教程,从入门到精通,R 字符串(10)

1、R 字符串

R 语言字符串可以使用一对单引号 ' ' 或一对双引号 " " 来表示。

单引号字符串中可以包含双引号。

单引号字符串中不可以包含单引号。

双引号字符串中可以包含单引号。

双引号字符串中不可以包含双引号。

以下示例演示来字符串的使用:

r 复制代码
a <- '使用单引号'
print(a)
b <- "使用双引号"
print(b)
c <- "双引号字符串中可以包含单引号(') "
print(c)
d <- '单引号字符串中可以包含双引号(") '
print(d)

执行以上代码输出结果为:

r 复制代码
[1] "使用单引号"
[1] "使用双引号"
[1] "双引号字符串中可以包含单引号(') "
[1] "单引号字符串中可以包含双引号(\") "

1.1、字符串操作

以下我们来看下 R 语言一些内置函数对字符串对操作。

1.2、paste() 函数

paste() 函数用于使用指定对分隔符来对字符串进行连接,默认对分隔符为空格。

语法格式:

r 复制代码
paste(..., sep = " ", collapse = NULL)

参数说明:

... : 字符串列表

sep : 分隔符,默认为空格

collapse : 两个或者更多字符串对象根据元素对应关系拼接到一起,在字符串进行连接后,再使用 collapse 指定对连接符进行连接

r 复制代码
a <- "Google"
b <- 'Nhooo'
c <- "Taobao"
print(paste(a,b,c))
print(paste(a,b,c, sep = "-"))
print(paste(letters[1:6],1:6, sep = "", collapse = "="))
paste(letters[1:6],1:6, collapse = ".")

执行以上代码输出结果为:

r 复制代码
[1] "Google Nhooo Taobao"
[1] "Google-Nhooo-Taobao"
[1] "a1=b2=c3=d4=e5=f6"
[1] "a 1.b 2.c 3.d 4.e 5.f 6"

1.3、format() 函数

format() 函数用于格式化字符串,format() 可作用于字符串或数字。

语法格式:

r 复制代码
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))

参数说明:

x : 输入对向量

digits : 显示的位数

nsmall : 小数点右边显示的最少位数

scientific : 设置科学计数法

width : 通过开头填充空白来显示最小的宽度

justify:设置位置,显示可以是左边、右边、中间等。

r 复制代码
# 显示 9 位,最后一位四舍五入
result <- format(23.123456789, digits = 9)
print(result)

# 使用科学计数法显示
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
# 小数点右边最小显示 5 位,没有的以 0 补充
result <- format(23.47, nsmall = 5)
print(result)
# 将数字转为字符串
result <- format(6)
print(result)
# 宽度为 6 位,不够的在开头添加空格
result <- format(13.7, width = 6)
print(result)
# 左对齐字符串
result <- format("Nhooo", width = 9, justify = "l")
print(result)
# 居中显示
result <- format("Nhooo", width = 10, justify = "c")
print(result)

执行以上代码输出结果为:

r 复制代码
[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Nhooo   "
[1] "  Nhooo  "

1.4、nchar() 函数

nchar() 函数用于计数字符串或数字列表的长度。

语法格式:

r 复制代码
nchar(x)

参数说明:

x : 向量或字符串

r 复制代码
result <- nchar("Google Nhooo Taobao")
print(result)

执行以上代码输出结果为:

r 复制代码
[1] 20

1.5、toupper() & tolower() 函数

toupper() & tolower() 函数用于将字符串的字母转化为大写或者小写。

语法格式:

r 复制代码
toupper(x)
tolower(x)

参数说明:

x : 向量或字符串

#转大写

r 复制代码
result <- toupper("Nhooo")
print(result)
# 转小写
result <- tolower("Nhooo")
print(result)

执行以上代码输出结果为:

r 复制代码
[1] "NHOOO"
[1] "nhooo"

1.6、substring() 函数

substring() 函数用于截取字符串。

语法格式:

r 复制代码
substring(x,first,last)

参数说明:

x : 向量或字符串

first : 开始截取的位置

last: 结束截取的位置

r 复制代码
# 从第 2 位截取到第 5 位
result <- substring("Nhooo", 2, 5)
print(result)

执行以上代码输出结果为:

r 复制代码
[1] "hooo"
相关推荐
belldeep2 小时前
python:genesis-world, Genesis-Embodied-AI
人工智能·python·ai·genesis
布莱克6053 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
LabVIEW开发3 小时前
LabVIEW 局部变量为何引发竞争条件?四种同步机制
开发语言·labview
全栈陈序员3 小时前
【Git】从入门到精通(五)—— git add:将文件加入暂存区
大数据·git·elasticsearch
benchmark_cc3 小时前
A 股、港股、美股如何建立统一的标的主数据格式?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
维克兜率天4 小时前
【维克】趋势型CTA:期货市场的“追涨杀跌“
人工智能·python
前进的李工4 小时前
Agent 学习笔记 05:系统提示词应该怎么写
笔记·学习·prompt·agent·个人开发·few-shot·ai应用开发
Zenova EdgeOS5 小时前
Go 工业边缘错误处理实战:用 errors 构建可追溯错误链
开发语言·数据库·golang
KKKlucifer5 小时前
国内数据安全服务的产品智能化转型:大模型赋能下的技术能力重构分析
大数据·数据库·重构
取名真是5 小时前
Python 算法知识点从基础到进阶
开发语言·python·算法