我是靠谱客的博主 体贴雨,这篇文章主要介绍R语言字符串知识点总结及实例分析,现在分享给大家,希望可以做个参考。

在R语言中的单引号或双引号对中写入的任何值都被视为字符串。 R语言存储的每个字符串都在双引号内,即使是使用单引号创建的依旧如此。

在字符串构造中应用的规则

  • 在字符串的开头和结尾的引号应该是两个双引号或两个单引号。它们不能被混合。
  • 双引号可以插入到以单引号开头和结尾的字符串中。
  • 单引号可以插入以双引号开头和结尾的字符串。
  • 双引号不能插入以双引号开头和结尾的字符串。
  • 单引号不能插入以单引号开头和结尾的字符串。

有效字符串的示例

以下示例阐明了在 R 语言中创建字符串的规则。

复制代码
1
2
3
4
5
6
7
8
9
10
11
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
2
3
4
[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"

无效字符串的示例

复制代码
1
2
3
4
5
6
7
8
e <- 'Mixed quotes" print(e) f <- 'Single quote ' inside single quote' print(f) g <- "Double quotes " inside double quotes" print(g)

当我们运行脚本失败给下面的结果。

复制代码
1
2
3
4
5
6
7
...: unexpected INCOMPLETE_STRING .... unexpected symbol 1: f <- 'Single quote ' inside unexpected symbol 1: g <- "Double quotes " inside

字符串操作

连接字符串 - paste() 函数

R语言中的许多字符串使用 paste() 函数组合。 它可以采取任何数量的参数组合在一起。

语法

对于粘贴功能的基本语法是

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

以下是所使用的参数的说明 -

  • ... 表示要组合的任意数量的自变量。
  • sep 表示参数之间的任何分隔符。它是可选的。
  • collapse 用于消除两个字符串之间的空格。 但不是一个字符串的两个字内的空间。

复制代码
1
2
3
4
5
6
7
8
9
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
2
3
[1] "Hello How are you? " [1] "Hello-How-are you? " [1] "HelloHoware you? "

格式化数字和字符串 - format() 函数

可以使用 format() 函数将数字和字符串格式化为特定样式。

语法

格式化函数的基本语法是

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

以下是所使用的参数的描述 -

  • x 是向量输入。
  • digits 是显示的总位数。
  • nsmall 是小数点右边的最小位数。
  • 科学设置为 TRUE 以显示科学记数法。
  • width 指示通过在开始处填充空白来显示的最小宽度。
  • justify 是字符串向左,右或中心的显示。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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
2
3
4
5
6
7
[1] "23.1234568" [1] "6.000000e+00" "1.314521e+01" [1] "23.47000" [1] "6" [1] " 13.7" [1] "Hello " [1] " Hello "

计算字符串中的字符数 - nchar() 函数

此函数计算字符串中包含空格的字符数。

语法

nchar() 函数的基本语法是

复制代码
1
nchar(x)

以下是所使用的参数的描述 -

x 是向量输入。

复制代码
1
2
result <- nchar("Count the number of characters") print(result)

当我们执行上面的代码,它产生以下结果

复制代码
1
[1] 30

更改case - toupper()和tolower()函数

这些函数改变字符串的字符的大小写。

语法

toupper()和tolower()函数的基本语法是

复制代码
1
2
toupper(x) tolower(x)

以下是所使用的参数的描述 -

x是向量输入。

复制代码
1
2
3
4
5
6
7
# Changing to Upper case. result <- toupper("Changing To Upper") print(result) # Changing to lower case. result <- tolower("Changing To Lower") print(result)

当我们执行上面的代码,它产生以下结果

提取

复制代码
1
2
[1] "CHANGING TO UPPER" [1] "changing to lower"

字符串的一部分 - substring()函数

此函数提取字符串的部分。

语法

substring() 函数的基本语法是

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

以下是所使用的参数的描述 -

  • x 是字符向量输入。
  • 首先是要提取的第一个字符的位置。
  • last 是要提取的最后一个字符的位置。

复制代码
1
2
3
# Extract characters from 5th to 7th position. result <- substring("Extract", 5, 7) print(result)

当我们执行上面的代码,它产生以下结果

复制代码
1
[1] "act"

到此这篇关于R语言字符串知识点总结及实例分析的文章就介绍到这了,更多相关R语言字符串内容请搜索靠谱客以前的文章或继续浏览下面的相关文章希望大家以后多多支持靠谱客!

最后

以上就是体贴雨最近收集整理的关于R语言字符串知识点总结及实例分析的全部内容,更多相关R语言字符串知识点总结及实例分析内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(170)

评论列表共有 0 条评论

立即
投稿
返回
顶部