R - 字符串
在 R 中,用一对单引号或双引号括起来的任何值都被视为字符串。R 内部将所有字符串存储为双引号包围,即使你使用单引号创建它们。
字符串构造规则
字符串开头和结尾的引号应均为双引号或均为单引号,不能混用。
可以在以单引号开头和结尾的字符串中插入双引号。
可以在以双引号开头和结尾的字符串中插入单引号。
不能在以双引号开头和结尾的字符串中插入双引号。
不能在以单引号开头和结尾的字符串中插入单引号。
有效字符串示例
以下示例阐明了在 R 中创建字符串的规则。
a <- 'Start and end with single quote' print(a) b <- "Start and end with double quotes" print(b) c <- "single quote ' in between double quotes" print(c) d <- 'Double quotes " in between single quote' print(d)
运行上述代码后,我们将得到以下输出 −
[1] "Start and end with single quote" [1] "Start and end with double quotes" [1] "single quote ' in between double quote" [1] "Double quote \" in between single quote"
无效字符串示例
e <- 'Mixed quotes" print(e) f <- 'Single quote ' inside single quote' print(f) g <- "Double quotes " inside double quotes" print(g)
运行该脚本时会失败,并给出以下结果。
Error: unexpected symbol in: "print(e) f <- 'Single" Execution halted
字符串操作
连接字符串 - paste() 函数
R 中的许多字符串使用 paste() 函数进行组合。它可以接受任意数量的参数进行组合。
语法
paste 函数的基本语法为 −
paste(..., sep = " ", collapse = NULL)
以下是所用参数的描述 −
... 表示任意数量待组合的参数。
sep 表示参数之间的分隔符。它是可选的。
collapse 用于消除两个字符串之间的空格。但不会消除一个字符串中两个单词之间的空格。
示例
a <- "Hello" b <- 'How' c <- "are you? " print(paste(a,b,c)) print(paste(a,b,c, sep = "-")) print(paste(a,b,c, sep = "", collapse = ""))
执行上述代码时,会产生以下结果 −
[1] "Hello How are you? " [1] "Hello-How-are you? " [1] "HelloHoware you? "
格式化数字和字符串 - format() 函数
可以使用 format() 函数将数字和字符串格式化为特定样式。
语法
format 函数的基本语法为 −
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
以下是所用参数的描述 −
x 是向量输入。
digits 是显示的总位数。
nsmall 是小数点右边的最小位数。
scientific 设置为 TRUE 以显示科学记数法。
width 表示通过在开头填充空白来显示的最小宽度。
justify 表示字符串向左、向右或居中显示。
示例
# 显示的总位数。最后一位四舍五入。
result <- format(23.123456789, digits = 9)
print(result)
# 以科学记数法显示数字。
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
# 小数点右边的最小位数。
result <- format(23.47, nsmall = 5)
print(result)
# format 将一切视为字符串。
result <- format(6)
print(result)
# 数字在开头用空白填充以达到宽度。
result <- format(13.7, width = 6)
print(result)
# 左对齐字符串。
result <- format("Hello", width = 8, justify = "l")
print(result)
# 居中对齐字符串。
result <- format("Hello", width = 8, justify = "c")
print(result)
执行上述代码时,会产生以下结果 −
[1] "23.1234568" [1] "6.000000e+00" "1.314521e+01" [1] "23.47000" [1] "6" [1] " 13.7" [1] "Hello " [1] " Hello "
计算字符串中的字符数 - nchar() 函数
此函数计算字符串中包括空格在内的字符数。
语法
nchar() 函数的基本语法为 −
nchar(x)
以下是所用参数的描述 −
x 是向量输入。
示例
result <- nchar("Count the number of characters")
print(result)
执行上述代码时,会产生以下结果 −
[1] 30
更改大小写 - toupper() 和 tolower() 函数
这些函数更改字符串中字符的大小写。
语法
toupper() 和 tolower() 函数的基本语法为 −
toupper(x) tolower(x)
以下是所用参数的描述 −
x 是向量输入。
示例
# 更改为大写。
result <- toupper("Changing To Upper")
print(result)
# 更改为小写。
result <- tolower("Changing To Lower")
print(result)
执行上述代码时,会产生以下结果 −
[1] "CHANGING TO UPPER" [1] "changing to lower"
提取字符串的部分 - substring() 函数
此函数提取字符串的部分。
语法
substring() 函数的基本语法为 −
substring(x,first,last)
以下是所用参数的描述 −
x 是字符向量输入。
first 是要提取的第一个字符的位置。
last 是要提取的最后一个字符的位置。
示例
# 从第 5 个位置到第 7 个位置提取字符。
result <- substring("Extract", 5, 7)
print(result)
执行上述代码时,会产生以下结果 −
[1] "act"