博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
字符函数和字符串函数_R中的字符串–函数及其操作
阅读量:2533 次
发布时间:2019-05-11

本文共 7143 字,大约阅读时间需要 23 分钟。

字符函数和字符串函数

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 quotesy<-"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:

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

Let’s see how this works.

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

#creates a stringw<-'welcome'x<-'to'y<-'Journal dev'z<-'R programming tutorials'#concatenates the strings in to a single stringpaste(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 valuesdf<-format(c(23,67.890),scientific = T)

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

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

#formats the decimal valuesformat(34.8,nsmall = 5)

Output = “34.80000”

输出=“ 34.80000”

#formats the number spaceformat(34.8,width=10)

Output = ” 34.8″

输出=“ 34.8”

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

Output = “JD “

输出=“ JD”

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

Output = ” JD”

输出=” JD”

##formats the number alignment to centreformat("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 .

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

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:

更多研究:

翻译自:

字符函数和字符串函数

转载地址:http://lnlzd.baihongyu.com/

你可能感兴趣的文章
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>