English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

R Strings

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

  • Single quotes can contain double quotes.

  • Single quotes cannot contain single quotes.

  • Double quotes can contain single quotes.

  • Double quotes cannot contain double quotes.

The following examples demonstrate the use of string concatenation:

a <- Use single quotes
print(a)
b <- Use double quotes
print(b)
c <- Double quotes can contain single quotes (')
print(c)
d <- 'Double quotes can be included in single quotes (\")'
print(d)

The output of the above code is:

[1]) "Use single quotes"
[1]) "Use double quotes"
[1]) "Single quotes can be included in double quotes ('')"
[1]) "Double quotes can be included in single quotes (\")"

String operations

Let's take a look at some built-in functions of R language for string operations.

paste() function

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

Syntax Format:

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

Parameter Description:

  • ...: String list

  • sep: Separator, default is space

  • collapse: Concatenate two or more string objects based on element correspondence, then use collapse to connect the connector after the string is connected

a <- "Google"
b <- 'w3codebox'
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 above code is:

[1]) "Google w3codebox Taobao"
[1]) "Google-w3codebox-Taobao"
[1]) "a1=b2=c3=d4=e5=f6"
[1]) "a 1.b 2.c 3.d 4.e 5.f 6"

format() function

The format() function is used to format strings, and format() can act on strings or numbers.

Syntax Format:

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

Parameter Description:

  • x: Input for vector

  • digits: The number of digits to display

  • nsmall: The minimum number of digits to display after the decimal point

  • scientific: Set scientific notation

  • width: Display the minimum width by filling spaces at the beginning

  • justify: Set position, the display can be left, right, center, etc.

# Display 9 digits, the last digit rounded
result  <- format(23.123456789, digits = 9)
print(result)
# Use scientific notation to display
result  <- format(c(6, 13.14521)), scientific = TRUE)
print(result)
#  Minimum display of decimal points to the right 5 spaces, fill with 0 if none
result  <- format(23.47,  nsmall = 5)
print(result)
#  Convert numbers to strings
result  <- format(6)
print(result)
#  Width is 6 spaces, add spaces at the beginning if not enough
result  <- format(13.7,  width = 6)
print(result)
#  Left-align string
result  <- format("w3codebox,  width = 9,  justify =  "l")
print(result)
#  Centered display
result  <- format("w3codebox,  width = 10,  justify =  "c")
print(result)

The output of the above code is:

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

nchar() Function

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

Syntax Format:

nchar(x)

Parameter Description:

  • x: Vector or string

result  <- nchar("Google  w3codebox Taobao)
print(result)

The output of the above code is:

[1] 20

toupper() & tolower() functions

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

Syntax Format:

toupper(x)
tolower(x)

Parameter Description:

  • x: Vector or string

# Convert to uppercase

result  <- toupper("w3codebox)
print(result)
#  Convert to lowercase
result  <- tolower("w3codebox)
print(result)

The output of the above code is:

[1]  "w3codebox
[1]  "w3codebox

substring() function

The substring() function is used to extract strings.

Syntax Format:

substring(x,first,last)

Parameter Description:

  • x: Vector or string

  • first: Starting cutting position

  • last: End of the cutting position

#  From the 2 bit cut to the 5 bit
result  <- substring("w3codebox 2, 5)
print(result)

The output of the above code is:

[1]  "hooo"