R语言中字符串有哪些知识点

发布时间:2021-03-29 09:26:42 作者:小新
来源:亿速云 阅读:220

这篇文章主要介绍了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(..., sep = " ", collapse = NULL)

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

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(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))

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

# 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() 函数的基本语法是

nchar(x)

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

x 是向量输入。

result <- nchar("Count the number of characters")
print(result)

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

[1] 30

更改case - 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)

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

# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)

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

[1] "act"

感谢你能够认真阅读完这篇文章,希望小编分享的“R语言中字符串有哪些知识点”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. R语言表达式知识点有哪些
  2. R语言常量知识点有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

r语言 字符串

上一篇:javaWeb怎么实现随机图片验证码

下一篇:MySQL中explain语句的使用方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》