r语言字符串转换为字符串_R语言中的字符串

r语言字符串转换为字符串

弦乐 (Strings)

The strings are defined as a group of characters. In the R language, the strings are commonly written in between either single or double-quotes.

字符串定义为一组字符。 在R语言中,字符串通常写在单引号或双引号之间。

The programmer who works with R needs to get acquainted with the rules while declaring and working with the concept of the strings. Therefore the below is the short description of the rules that are needed to be followed while using strings in the R language. One must make sure that strings are surrounded either with single quotes or double quotes on both sides. The string which starts with the single quotes can have double quotes in the middle of them and vice versa.

使用R的程序员在声明和使用字符串的概念时需要熟悉规则。 因此,以下是使用R语言的字符串时需要遵循的规则的简短描述。 必须确保字符串的两端都用单引号或双引号引起来。 以单引号开头的字符串可以在中间加上双引号,反之亦然。

Examples for the valid strings:

有效字符串的示例:

str1 <- 'String that starts with single quote and ends with single quote too'
print(str1)

str2 <- "String that starts with double quotes and ends with double quotes too"
print(str2)

str3 <- "String that has single quote marks ' inside double quotes"
print(str3)

str4 <- 'String that has Double quotes marks " inside single quote'
print(str4)

Output

输出量

[1] "String that starts with single quote and ends with single quote too"
[1] "String that starts with double quotes and ends with double quotes too"
[1] "String that has single quote marks ' inside double quotes"
[1] "String that has Double quotes marks \" inside single quote"

Now as we all know that when we have one part for anything then we always have the counterpart for the same thing and failing to do this will be treated as an invalid string. The following examples help the programmers in acknowledging the difference between the valid and invalid string syntax in the R language.

现在我们都知道,当我们为任何事物分配一部分时,我们总是拥有同一事物的对应部分,如果不这样做,将被视为无效字符串。 下面的示例帮助程序员确认R语言中有效和无效字符串语法之间的区别。

# The str1's string will not end until the second single quote is found...

str1 <- 'String that starts with single quote and ends with double quotes"
print(str1)

str2 <- 'string that has a Single quote marks ' inside single quote'
print(str2)

str3 <- "Double quotes marks " inside double quotes"
print(str3)

Output

输出量

Error: unexpected symbol in:
"
str2 

Explanation:

All the initializations were done in the wrong way that does not match the quotes properly. This Throws error and code does not execute properly.

String Manipulation

Concatenating strings using paste() function

In the R language, the programmer can combine two strings using the paste() function. The paste() function will support any number of arguments into it for the combining purpose.

Syntax:

Parameters:

Error: unexpected symbol in:
"
str2 

Explanation:

All the initializations were done in the wrong way that does not match the quotes properly. This Throws error and code does not execute properly.

String Manipulation

Concatenating strings using paste() function

In the R language, the programmer can combine two strings using the paste() function. The paste() function will support any number of arguments into it for the combining purpose.

Syntax:

Parameters:

  • ... : This exemplifies any emblems of arguments that can be integrated using the paste() function

    ... :这示例了可以使用paste()函数集成的参数的任何符号

  • sep : the separator between the arguments considered. However, the usage of a separate is always optional and it mainly depends on the user who works with this particular syntax.

    sep :所考虑参数之间的分隔符。 但是,分隔符的使用始终是可选的,并且主要取决于使用此特定语法的用户。

  • collapse : the keyword used for the purpose of elimination of the spaces that prevail between the considered number of strings. One should clearly note that space whatever is mentioned here is between the strings but not amid the two words of a string. The usage of collapse is also optional.

    塌陷 :用于消除考虑的字符串数之间占优势的空格的关键字。 一个人应该清楚地注意到,这里提到的任何空间都在字符串之间,但不在字符串的两个词之间。 折叠的用法也是可选的。

Given below is the example code for the demonstration of the above topic:

下面给出的是用于演示上述主题的示例代码:

part1 <- "Welcome"
part2 <- 'to'
part3 <- "includehelp.com"

str1 <- paste(part1, part2, part3)
print(str1)

str1 <- paste(part1, part2, part3, sep = "", collapse = "")
print(str1)

str1 <- paste(part1, part2, part3, sep = "-")
print(str1)

Output

输出量

[1] "Welcome to includehelp.com"
[1] "Welcometoincludehelp.com"
[1] "Welcome-to-includehelp.com"

使用format()函数格式化数字和字符串 (Formatting numbers and strings using format() function)

In the R, the numbers and strings can be formatted into the specific style with the help of format() function.

在R中,可以借助format()函数将数字和字符串格式化为特定样式。

Syntax:

句法:

    format(x, digits, nsmall, scientific, width, justify = c("left", "right", "centre", "none"))

Parameters:

参数:

  • x is the vector input which depends on the user input.

    x是矢量输入,取决于用户输入。

  • digits is the total number of the digits that will be displayed.

    digits是将显示的数字总数。

  • nsmall, this is the parameter which is nothing but the minimum number of digits after the decimal point to the right side.

    nsmall ,这是参数,不过是右边小数点后的最小位数。

  • scientific is generally set to TRUE with the main intention to display the scientific notations.

    通常将科学型设置为TRUE,主要目的是显示科学符号。

  • width demonstrates the minimum number of characters or numbers to be displayed by padding the blanks in the initial stages.

    width表示通过在初始阶段填充空格来显示的最小字符数或数字。

  • justify is the display option that displays the string on left, center or in the right positions of the screen.

    justify是一种显示选项,可在屏幕的左,中或右位置显示字符串。

Example 1:

范例1:

# We are formatting the total number of digits to be 
# considered as less than the total number of digits 
# in the value...

value <- 2345.12541132
result <- format(value, digits = 8)
print(result)

Output

输出量

[1] "2345.1254"

Example 2:

范例2:

# We will format the number to its scientific notation...

number <- 87106.912310
result <- format(number, scientific = TRUE)
print(result)

Output

输出量

[1] "8.710691e+04"

Example 3:

范例3:

# Using nsmall we will set the minimum number 
# of digits after the decimal point.

number <- 98.831
result <- format(number, nsmall = 7)
print(result)

Output

输出量

[1] "98.8310000"

Example 4:

范例4:

# Width added some blank spaces to the number...

number <- 89.1223
result <- format(number, width = 12)
print(result)

Output

输出量

[1] "     89.1223"

Example 5:

范例5:

# The justify along with width is used to add the black spaces 
# in the given direction = left(end of string) and 
# center(middle of the string)...

str = "includeHelp.com"

formatedString <- format(str , width = 34, justify = "l")
print(formatedString )
formatedString <- format(str , width = 34, justify = "c")
print(formatedString)

Output

输出量

[1] "includeHelp.com                   "
[1] "         includeHelp.com          "

在toupper()和tolower()的帮助下更改大小写 (Changing the case with the help of the toupper() and the tolower())

The toupper() and tolower() functions generally contribute to the program when there is a grave need of changing the case of the considered string. In that case, the programmers make use of these basic functions to make the code more comfortable for a user who uses it.

当迫切需要更改所考虑字符串的大小写时, toupper()tolower()函数通常有助于程序。 在那种情况下,程序员利用这些基本功能使代码对于使用它的用户来说更加舒适。

Syntax:

句法:

    toupper(x)
    tolower(x)

Parameters:

参数:

  • x is considered vector input.

    x被视为向量输入。

Example:

例:

str <- "Welcome to Includehelp.com"

# We will convert the string to uppercase 
# using toupper() method...
upperCase <- toupper(str)
print(upperCase)

# We will convert the string to lowercase 
# using tolower() method...
lowerCase <- tolower(str)
print(lowerCase)

Output

输出量

[1] "WELCOME TO INCLUDEHELP.COM"
[1] "welcome to includehelp.com"

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

nchar() function calculates the total number of characters encompassing spaces in the string considered for the purpose of counting the strings.

nchar()函数计算为计算字符串而考虑的字符串中包含空格的字符总数。

Syntax:

句法:

    nchar(x)

x: the parameter which is used in the above syntax is nothing but a vector output.

x :以上语法中使用的参数不过是向量输出。

Example:

例:

# We will count the number of characters using nchar...

str = "Welcome to Includehelp.com"
print(str)
charCount <- nchar(str)
print(charCount)

Output

输出量

[1] "Welcome to Includehelp.com"
[1] 26

使用substring()属性从考虑的字符串中提取部分 (Extracting the parts from the considered string using the substring() attribute)

The main purpose of making use of substring() function in the R is that it extracts a particular part of the string.

在R中使用substring()函数的主要目的是提取字符串的特定部分。

Syntax:

句法:

    Substring(x, first, last);

Parameters:

参数:

  • x is nothing but the string considered itself.

    x只是字符串本身。

  • first is the parameter that points to the position of the first character that needs to be extracted.

    first是指向需要提取的第一个字符的位置的参数。

  • last is the position that corresponds to the last character that is to be extracted from the considered string.

    last是与要从所考虑的字符串中提取的最后一个字符相对应的位置。

Example:

例:

# We will extract substring.

string <- "Learn programming at includehelp.com"
result <- substring(string, 22, 36)
print(result)

Output

输出量

[1] "includehelp.com"


翻译自: https://www.includehelp.com/r/strings.aspx

r语言字符串转换为字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值