字符函数和字符串函数





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)数组,其中包含单个或多个值。 字符串可以包括字符数据,数字数据以及任何特殊字符。



(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 R

df<-"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


(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 \”



(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.

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



(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() 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编程教程”



(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”



(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



(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)
  • tolower(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是一种统计分析语言”



(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

字符函数和字符串函数