Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R Strings

R Strings

In R, a string is a data type representing text data. It consists of characters (a character vector) and can include letters, digits, symbols, spaces, and other characters.

R strings can be represented by a pair of single quotes ' ' or a pair of double quotes " ".

** Creating a string:** You can create a string using single or double quotes.

The following example demonstrates the use of strings:

Example

a

<-

'Using single quotes'

print

(

a

)

b

<-

"Using double quotes"

print

(

b

)

c

<-

"A double-quoted string can contain single quotes (') "

print

(

c

)

d

<-

'A single-quoted string can contain double quotes (") '

print

(

d

)

The output of the code above is:



[1] "使用单引号"

[1] "使用双引号"

[1] "双引号字符串中可以包含单引号(') "

[1] "单引号字符串中可以包含双引号(\") "


String Operations

R provides a variety of functions and operators for manipulating strings, making it convenient to process and handle text data.

Below we look at some of R's built-in functions for string operations.

The paste() function

The paste() function is used to concatenate strings with a specified separator; the default separator is a space.

Syntax format:



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

Parameter Description:

Example

a

<-

"Google"

b

<-

'Runoob'

c

<-

"Taobao"

print

(

paste

(

a,b,

c

)

)

print

(

paste

(

a,b,

c

, sep

=

"-"

)

)

print

(

paste

(

letters

[

1

:

6

]

,

1

:

6

, sep

=

""

, collapse

=

"="

)

)

paste

(

letters

[

1

:

6

]

,

1

:

6

, collapse

=

"."

)

The output of the code above is:

[1] "Google Runoob Taobao"

[1] "Google-Runoob-Taobao"

[1] "a1=b2=c3=d4=e5=f6"

[1] "a 1.b 2.c 3.d 4.e 5.f 6"

The format() function

The format() function is used to format strings; format() can be applied to strings or numbers.

Syntax format:

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

Parameter Description:

Example

Display 9 digits, with the last one rounded

result

<-

format

(

23.123456789

, digits

=

9

)

print

(

result

)

Display using scientific notation

result

<-

format

(

c

(

6

,

13.14521

)

, scientific

=

TRUE

)

print

(

result

)

Display at least 5 digits to the right of the decimal point, padding with 0 where needed

result

<-

format

(

23.47

, nsmall

=

5

)

print

(

result

)

Convert a number to a string

result

<-

format

(

6

)

print

(

result

)

A width of 6 digits; add spaces at the beginning if it is shorter

result

<-

format

(

13.7

, width

=

6

)

print

(

result

)

Left-align the string

result

<-

format

(

"Runoob"

, width

=

9

, justify

=

"l"

)

print

(

result

)

Center the display

result

<-

format

(

"Runoob"

, width

=

10

, justify

=

"c"

)

print

(

result

)

The output of the code above is:

[1] "23.1234568"

[1] "6.000000e+00" "1.314521e+01"

[1] "23.47000"

[1] "6"

[1] "  13.7"

[1] "Runoob   "

[1] "  Runoob  "

The nchar() function

The nchar() function is used to count the length of a string or a list of numbers.

Syntax format:

nchar(x)

Parameter Description:

Example

result

<-

nchar

(

"Google Runoob Taobao"

)

print

(

result

)

The output of the code above is:

[1] 20

The toupper() & tolower() functions

The toupper() & tolower() functions convert the letters of a string to uppercase or lowercase.

Syntax format:

toupper(x)

tolower(x)

Parameter Description:

Convert to uppercase

Example

result

<-

toupper

(

"Runoob"

)

print

(

result

)

Lowercase conversion

result

<-

tolower

(

"Runoob"

)

print

(

result

)

The output of the code above is:

[1] "RUNOOB"

[1] "runoob"

The substring() function

The substring() function is used to extract part of a string.

Syntax format:

substring(x,first,last)

Parameter Description:

Example

Extract from position 2 to position 5

result

<-

substring

(

"Runoob"

,

2

,

5

)

print

(

result

)

The output of the code above is:

[1] "unoo"

String replacement

Use gsub() Function to replace specific characters or patterns in a string.

Example

str

<-

"Hello, World!"

new_str

<-

gsub

(

"World"

,

"R"

,

str

)

Output: "Hello, R!"

String splitting

Use strsplit() Function that splits a string into substrings.

Example

str

<-

"Hello, World!"

split_str

<-

strsplit

(

str

,

","

)

Output: List of 1

        [[1]]

        [1] "Hello"   " World!"

The above lists only a small portion of R string operation examples. R has a large number of string processing functions and operators, such as pattern matching, case conversion, and string comparison. You can consult R's official documentation for a fuller list and usage notes on string functions.