R 简明教程

R - Strings

在 R 中用一对单引号或双引号写的任何值都被视为一个字符串。内部上,R 将每一个字符串都存储在双引号中,即使您用单引号创建它们。

Any value written within a pair of single quote or double quotes in R is treated as a string. Internally R stores every string within double quotes, even when you create them with single quote.

Rules Applied in String Construction

  1. The quotes at the beginning and end of a string should be both double quotes or both single quote. They can not be mixed.

  2. Double quotes can be inserted into a string starting and ending with single quote.

  3. Single quote can be inserted into a string starting and ending with double quotes.

  4. Double quotes can not be inserted into a string starting and ending with double quotes.

  5. Single quote can not be inserted into a string starting and ending with single quote.

Examples of Valid Strings

以下示例说明了在 R 中创建字符串的规则。

Following examples clarify the rules about creating a string in 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)

运行以上代码时,我们将得到以下输出 −

When the above code is run we get the following output −

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

Examples of Invalid Strings

e <- 'Mixed quotes"
print(e)

f <- 'Single quote ' inside single quote'
print(f)

g <- "Double quotes " inside double quotes"
print(g)

当我们运行脚本时,它失败并给出以下结果。

When we run the script it fails giving below results.

Error: unexpected symbol in:
"print(e)
f <- 'Single"
Execution halted

String Manipulation

Concatenating Strings - paste() function

R 中使用 paste() 函数组合多个字符串。它可以接受任意数量的参数进行组合。

Many strings in R are combined using the paste() function. It can take any number of arguments to be combined together.

Syntax

paste 函数的基本语法为:

The basic syntax for paste function is −

paste(..., sep = " ", collapse = NULL)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. …​ represents any number of arguments to be combined.

  2. sep represents any separator between the arguments. It is optional.

  3. collapse is used to eliminate the space in between two strings. But not the space within two words of one string.

Example

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 = ""))

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

[1] "Hello How are you? "
[1] "Hello-How-are you? "
[1] "HelloHoware you? "

Formatting numbers & strings - format() function

可以使用 format() 函数将数字和字符串格式化为特定样式。

Numbers and strings can be formatted to a specific style using format() function.

Syntax

format 函数的基本语法为:

The basic syntax for format function is −

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

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is the vector input.

  2. digits is the total number of digits displayed.

  3. nsmall is the minimum number of digits to the right of the decimal point.

  4. scientific is set to TRUE to display scientific notation.

  5. width indicates the minimum width to be displayed by padding blanks in the beginning.

  6. justify is the display of the string to left, right or center.

Example

# 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)

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

[1] "23.1234568"
[1] "6.000000e+00" "1.314521e+01"
[1] "23.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "

Counting number of characters in a string - nchar() function

此函数计算字符串中包括空格在内的字符数。

This function counts the number of characters including spaces in a string.

Syntax

nchar() 函数的基本语法为:

The basic syntax for nchar() function is −

nchar(x)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is the vector input.

Example

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

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

[1] 30

Changing the case - toupper() & tolower() functions

这些函数更改字符串的字符的大小写。

These functions change the case of characters of a string.

Syntax

toupper() 和 tolower() 函数的基本语法为:

The basic syntax for toupper() & tolower() function is −

toupper(x)
tolower(x)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is the vector input.

Example

# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.
result <- tolower("Changing To Lower")
print(result)

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

[1] "CHANGING TO UPPER"
[1] "changing to lower"

Extracting parts of a string - substring() function

此函数提取字符串的部分信息。

This function extracts parts of a String.

Syntax

substring() 函数的基本语法为:

The basic syntax for substring() function is −

substring(x,first,last)

以下是所用参数的描述 -

Following is the description of the parameters used −

  1. x is the character vector input.

  2. first is the position of the first character to be extracted.

  3. last is the position of the last character to be extracted.

Example

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

当我们执行上述代码时,会产生以下结果 -

When we execute the above code, it produces the following result −

[1] "act"