在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 quotes \" 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)
输出:
错误: unexpected symbol 于
"
f <- 'Single"
Error in print(f) : 找不到对象'f'
错误: unexpected symbol在"g <- "Double quotes " inside"里
字符串操作
连接字符串—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? "
可以看到,若不填写参数seq,R语言中默认在两个连接的字符串中间添加一个空格(“ ”)。
格式化数字和字符串—format()函数
作用:将数字和字符串格式化为特定样式。
语法:
format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))
x:向量输入
digits:显示的总位数
nsmall:小数点右边的最小位数
scientific:此处若为TRUE,则显示科学记数法
width:指示通过在开始处填充空白来显示的最小宽度(向左填充空白)
justify:字符串向左、向右或中心显示(justify=“l” or “r” or “c” or “n”)
示例:
输入:
# Total number of digits displayed. Last digit rounded off.
result <- format(23.123456789, digits = 9)
print(result)
# Display numbers in scientific notation.
result <- format(c(6, 13.14521), scientific = TRUE)
print(result)
# The minimum number of digits to the right of the decimal point.
result <- format(23.47, nsmall = 5)
print(result)
# Format treats everything as a string.
result <- format(6)
print(result)
# Numbers are padded with blank in the beginning for width.
result <- format(13.7, width = 6)
print(result)
# Left justify strings.
result <- format("Hello", width = 8, justify = "l")
print(result)
# Justfy string with center.
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(x)
x:向量输入
示例:
输入:
result <- nchar("Count the number of characters")
print(result)
输出:
[1] 30
更改字符串大小写—toupper()和tolower()函数
作用:改变字符串的字符的大小写
语法:
toupper(x)
tolower(x)
x:向量输入
示例:
输入:
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)
# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)
输出:
[1] "CHANGING TO UPPER"
[1] "changing to lower"
提取字符串的一部分—substring()函数
作用:提取字符串的部分
语法:
substring(x,first,last)
x:字符向量输入
first:要提取的第一个字符的位置
last:要提取的最后一个字符的位置
示例:
输入:
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
输出:
[1] "act"
个人觉得,以上各函数与C++中相应函数的用法类似。