字符函数和字符串函数

Strings are generally a one-dimensional (1D) arrays that contain single or multiple values in it. Strings can include character data, numerical data, and any special characters as well.

字符串通常是一维(1D)数组,其中包含单个或多个值。 字符串可以包括字符数据,数字数据以及任何特殊字符。



R中的字符串–简介 (Strings in R – A brief introduction )

A string is nothing but a value which is enclosed by double quotes “” in R. It can be a single value or a multiple. Meanwhile, the empty strings are represented by ” “. You should represent the strings in double quotes ” ” and can use single quotes in between the string as well.

字符串不过是R中用双引号“”引起来的值。它可以是单个值或多个。 同时,空字符串用“”表示。 您应该用双引号“”表示字符串,也可以在字符串之间使用单引号。

A simple illustration of the string is given below.

字符串的简单说明如下。


#A simple string in Rdf<-"journal dev - R tutorials"

Note: You cannot use double quotes inside the double-quoted and single quotes inside single-quoted strings.

注意:不能在双引号内使用双引号,也不能在单引号字符串内使用单引号。



字符串构造规则 (String construction rules)

There are some general rules are there to construct a string.

这里有一些构造字符串的通用规则。

  • The beginning and end quotes should be the same开头和结尾引号应该相同
  • You can use single quotes in between double-quotes您可以在双引号之间使用单引号
  • You can use double quotes in between single quotes您可以在单引号之间使用双引号
  • You cannot use single quotes in between single quotes您不能在单引号之间使用单引号
  • You cannot use double quotes in between double-quotes您不能在双引号之间使用双引号


R中的有效字符串 (Valid strings in R)

Valid strings are the strings which followed and constructed based on the string rules. Here are some examples of valid strings are shown below.

有效字符串是根据字符串规则遵循并构造的字符串。 以下是一些有效字符串的示例。

x<- "ANOVA in R"

Output = “ANOVA in R”

输出=“ R中的ANOVA”


y<-'www.journaldev.com'

Output = “www.journaldev.com”

输出=“ www.journaldev.com”


df<- "R tutorials 'R programming'in JD"

Output = “R tutorials ‘R programming’in JD”

输出=“ JD中的R教程'R编程'”


df<-'string "function" in R'

Output = “string \”function\” in R”

输出=“ R中的字符串“ function \”



R中的无效字符串 (Invalid strings in R)

If any string violates the rules then it will become an invalid string. Some of the invalid strings are listed below.

如果有任何字符串违反规则,则它将成为无效的字符串。 下面列出了一些无效的字符串。


#quotes mix up
x<- "ANOVA in R'

Output =

输出=

Error: unexpected symbol in:

x<- “ANOVA”

错误:出现意外符号:

x <-“方差分析”


#use of double quotes inside double quotes
y<-"R programming "tutorials" is here"

Output = Error: unexpected symbol in “y<-“R programming “tutorials”

输出=错误:“ y <-” R编程“教程”中的意外符号

As you can see the above examples, you will get to know about the errors and invalid strings as well.

正如您可以看到上面的示例,您还将了解有关错误和无效字符串的信息。



R中的字符串操作 (String manipulation in R)

In this section, we are going to manipulate the strings in R in various aspects as shown below.

在本节中,我们将在各个方面对R中的字符串进行操作,如下所示。

  • paste() – To concatenate the strings.paste() –连接字符串。
  • format() – To format the numerical values.format()–格式化数值。
  • nchar() – To count the characters in the string.nchar()–计算字符串中的字符。
  • substring() – To extract the specific characters from the string.substring()–从字符串中提取特定字符。

We will see how these above mentioned functions manipulate the strings.

我们将看到上述这些函数如何操作字符串。



paste()函数连接字符串。 (paste() function to concatenate strings.)

Using paste() function, you can easily concatenate or combine the strings. The paste() function is capable of taking multiple elements as inputs and concatenates them into a string.

使用paste()函数,您可以轻松地连接或组合字符串。 paste()函数能够将多个元素作为输入并将它们连接成一个字符串。

To learn more about paste() function in R: paste() in R

要了解有关R中的paste()函数的更多信息:R中的paste()

Let’s see how this works.

让我们看看它是如何工作的。


#creates a string
w<-'welcome'
x<-'to'
y<-'Journal dev'
z<-'R programming tutorials'#concatenates the strings in to a single string
paste(w,x,y,z,sep = '_')

Output = “welcome_to_Journal dev_R programming tutorials”

输出=“ welcome_to_Journal dev_R编程教程”



在R中使用format()函数进行字符串格式化 (String formatting using format() function in R)

In R you can format the numbers in various aspects as shown below. Let’s see how format() function will work.

在R中,您可以在各个方面设置数字格式,如下所示。 让我们看看format()函数如何工作。


#formats the number count
df<-format(23.45788,digits = 5)

Output = “23.458”

输出=“ 23.458”


#formats the scientific values
df<-format(c(23,67.890),scientific = T)

Output = “2.300e+01” “6.789e+01”

输出=“ 2.300e + 01”“ 6.789e + 01”


#formats the decimal values
format(34.8,nsmall = 5)

Output = “34.80000”

输出=“ 34.80000”


#formats the number space
format(34.8,width=10)

Output = ” 34.8″

输出=“ 34.8”


#formats the number alignment to left
format("JD",width = 10,justify = 'l')

Output = “JD “

输出=“ JD”


#formats the number alignment to right
format("JD",width = 10,justify = 'r')

Output = ” JD”

输出=” JD”


##formats the number alignment to centre
format("JD",width = 10,justify = 'c')

Output = ” JD “

输出=“ JD”



使用nchar()函数计算字符串中的字符数 (Count the number of characters in a string using nchar() function)

R has the function named nchar() which counts the number of characters present in a given string as well. Let’s see how it works.

R具有名为nchar()的函数,该函数还计算给定字符串中存在的字符数。 让我们看看它是如何工作的。

Syntax:

句法:


nchar(x)

Let me show you how you can count the number characters present in a string using nchar().

让我向您展示如何使用nchar()计算字符串中存在的数字字符。


nchar("R programming tutorials")

Output = 23

输出= 23


nchar("R is a statistical analysis language")

Output = 36

输出= 36



使用toupper()和tolower()函数更改字符串的大小写 (Change the case of the string using toupper() and tolower() functions)

In R you can easily change the case of the string from upper to lower or vice-versa using the tolower() and toupper() functions.

在R中,您可以使用tolower()和toupper()函数轻松地将字符串的大小写从大写改为小写,反之亦然。

The syntax is given below,

语法如下:

  • toupper(x)上衣(x)
  • tolower(x)降低(x)

Where, x = input string

其中, x =输入字符串


toupper("R is a statistical analysis language")

Output = “R IS A STATISTICAL ANALYSIS LANGUAGE”

输出=“ R是一种统计分析语言”


tolower("R IS A STATISTICAL ANALYSIS LANGUAGE")

Output = “r is a statistical analysis language”

输出=“ r是一种统计分析语言”



R中的substring()函数 (The substring() function in R)

The substring() function in R is used to extract the data or the characters from a string. The below illustrations will define the working of substring() in R.

R中的substring()函数用于从字符串中提取数据或字符。 下图将定义R中substring()的工作方式。


#extractes the specific charater range from the string
substring("journaldev",8,11)

Output = “dev”

输出=“ dev”

In the substring() function, the first number and second number indicates the beginning and end of the index number which you want to extract from the string as shown above.

在substring()函数中,第一个数字和第二个数字表示要从字符串中提取的索引号的开头和结尾,如上所示。


#extractes the specific charater range from the string
substring("journaldev",1,7)

Output = “journal”

输出=“新闻”

Note: The index number starts from 1.

注意:索引号从1开始。



结语 (Wrapping up)

Being a fantastic statistical language, R offers various functions function for data manipulation. In this tutorial, we have focussed on the string and its operations in R.

作为一种出色的统计语言,R提供了用于数据操作的各种函数功能。 在本教程中,我们专注于R中的字符串及其操作。

You can use paste(), format(), nchar() and substring() functions to manipulate the strings as discussed in the above sections.

您可以使用paste(),format(),nchar()和substring()函数来操纵字符串,如上节所述。

Please be aware of the rules of string construction whenever you construct a string. That’s all about string and its operations in R. Happy learning!!!

每当构造字符串时,请注意字符串构造的规则。 这就是关于字符串及其在R中的操作的所有内容。 祝您学习愉快!!!

More study: R documentation

更多研究: R文档

翻译自: https://www.journaldev.com/40528/string-in-r

字符函数和字符串函数

字符函数和字符串函数_R中的字符串–函数及其操作相关推荐

  1. matlab中floor函数,floor函数_怎么在excel中使用floor函数

    floor函数即上取整函数,是计算机C语言中的数学函数,与ceil函数相对应.但是它在excel中却是另一种含义,FLOOR函数是向下舍入为最接近指数基数的倍数,下面小编就教你怎么在excel中使用f ...

  2. oracle数据存储过程 中的循环 for 拼接字符串,oracle存储过程中使用字符串拼接

    1.使用拼接符号"||" v_sql := 'SELECT * FROM UserInfo WHERE ISDELETED = 0 AND ACCOUNT =''' || vAcc ...

  3. MATLAB 存放字符串(循环中调用字符串)

    MATLAB 存放字符串(循环中调用字符串) str=['asdfs';'sdadas';'qweqweqwe'];for i=1:3str(i,:) end 用分号隔开就可以调用了,比如在写图片标题 ...

  4. deliphi 字符串分割_delphi中拆分字符串的函数

    delphi的字符截取函数LeftStr, MidStr, RightStr拆分字符串 这几个函数都包含在StrUtils中,所以需要uses StrUtils; 假设字符串是 Dstr := 'De ...

  5. str split函数 php,怎么在php中利用str_split函数分割字符串

    怎么在php中利用str_split函数分割字符串 发布时间:2021-01-04 14:52:31 来源:亿速云 阅读:54 作者:Leah 今天就跟大家聊聊有关怎么在php中利用str_split ...

  6. c语言sub函数是什么,C语言中的字符串截取函数

    C语言中的字符串截取函数及应用 /*======================================================== 子数整数 源程序名 num.??? (pas,c, ...

  7. oracle plsql 字符串长度,plsql中常用字符串函数

    1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space f ...

  8. php 正则 最后一个字符,关于正则表达式:在PHP中查找字符串中的最后一个字符...

    我正在用PHP进行一些URL重写,需要找到末尾有斜线的URL,然后执行301重定向.我以为会有一个简单的PHP函数来查找最后一个字符串,但我找不到任何东西.第一直觉让我觉得我需要使用regex,但我不 ...

  9. excel中vlookup函数的使用方法_EXCEL中查找匹配函数VLOOKUP使用技巧

    1.VLOOKUP基础用法 VLOOKUP 函数表示: = VLOOKUP (你想要查找的内容,要查找的位置,包含要返回的值的区域中的列号,返回近似或精确匹配-表示为 1/TRUE 或 0/假). 第 ...

最新文章

  1. VS Code – No source control providers 解决方法
  2. 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
  3. OpenCV图像处理使用笔记(七)——卷积运算原理
  4. pack_padded_sequence 和 pad_packed_sequence
  5. 日志规范之阿里巴巴开发手册中的其它规范讲解
  6. 关于CKeditor的个性应用设置 转
  7. SQL Server 空间监测
  8. 无法连接NVIDIA驱动:NVIDIA-SMI has failed because it couldn’t communicate with the NVIDIA driver
  9. 2006 飞行员配对(二分图最大匹配)
  10. android 能否控制drawabletop的大小_V038小程序能否逐步完全取代APP?
  11. python中temp是什么意思_.temp(temp是什么意思?)
  12. 易连云打印机PHP接口
  13. 用Burg法估计AR模型的参数原理详解及matlab实现
  14. Golang的文件处理方式-常见的读写
  15. 特征码的使用办法_如何查询使用车架号查询车辆是否是事故
  16. parameterType 用法
  17. qute图(女生勿进
  18. Layui表单复选框验证
  19. 计算机多媒体自荐书,计算机多媒体专业自荐信模板.doc
  20. 终极解决大黄峰黑屏加信号问题,觉得有用请顶我

热门文章

  1. Shell 常用积累
  2. Robocode教程4——Robocode的游戏物理
  3. #语音信号处理基础(十一)——梅尔倒谱系数的提取
  4. [转载] Python中不可变集合的使用frozenset()方法
  5. [转载] python numpy.sqrt_python中numpy库ndarray多维数组的的运算:np.abs(x)、np.sqrt(x)、np.modf(x)等...
  6. [转载] Java基础知识面试题(2020最新版)
  7. Xilinx Altera FPGA中的逻辑资源(Slices VS LE)比较
  8. 端口截听实现端口隐藏 嗅探与攻击
  9. Description Resource Path LocationType Java compiler level does not match the version of the instal
  10. Centos 7 Puppet之foreman介绍安装测试