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)
当上述代码运行时,我们得到以下的输出:
...: unexpected INCOMPLETE_STRING
.... unexpected symbol
1: f <- 'Single quote ' inside
unexpected symbol
1: g <- "Double quotes " inside
字符串操作
连接字符串 - 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 - 是字符串显示在左边,右边或中心
示例
# 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 "
统计字符串的字符数 - ncahr()函数
函数计算字符数量,包括在一个字符串的空格的个数。
语法
nchar()函数的基本语法是:
nchar(x)
以下是所使用的参数的说明:
x - 向量输入。
示例
result <- nchar("Count the number of characters")
print(result)
当我们上面的代码执行时,它产生以下结果:
[1] 30
改变大小写 - toupper()和 tolower()函数
这些函数改变字符串的字符的大小写。
语法
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()函数的基本语法是:
substring(x,first,last)
以下是所使用的参数的说明:
x - 是字符向量输入。
first - 是第一个字符要被提取的位置。
last - 是最后一个字符要被提取的位置。
示例
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
当我们上面的代码执行时,它产生以下结果:
[1] "act"
数据分析咨询请扫描二维码