Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

R language string


May 12, 2021 R language tutorial


Table of contents


Any value written in a single or double quote pair in the R language is considered a string. E ach string stored in the R language is in double quotes, even if it is created with single quotes.

Rules applied in string construction

  • The quotation marks at the beginning and end of the string should be two double quotes or two single quotes. T hey cannot be mixed.

  • Double quotes can be inserted into strings that begin and end with single quotes.

  • Single quotes can insert strings that begin and end with double quotes.

  • Double quotes cannot be inserted into strings that begin and end with double quotes.

  • Single quotes cannot be inserted into strings that begin and end with single quotes.

An example of a valid string

The following example illustrates the rules for creating strings in the R language.

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 we run the code above, 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"

An example of an invalid string

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 failed to give the following results.

...: unexpected INCOMPLETE_STRING

.... unexpected symbol 
1: f <- 'Single quote ' inside

unexpected symbol
1: g <- "Double quotes " inside

String operation

Connection string - paste() function

Many strings in the R language use a combination of paste() functions. I t can take any number of parameters combined.

Grammar

The basic syntax for the paste function is -

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

The following is a description of the parameters used -

  • ... R epresents any number of arguments to combine.

  • Sep represents any separator between arguments. I t is optional.

  • collapse is used to eliminate spaces between two strings. B ut not a string within two words of space.

Cases

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 code above, it produces the following results -

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

Format numbers and strings - format() functions

You can use the format() function to format numbers and strings into a specific style.

Grammar

The basic syntax of formatting functions is -

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

The following is a description of the parameters used -

  • x is the vector input.

  • digits are the total number of digits displayed.

  • nsmall is the smallest number of digits to the right of the scale.

  • Science is set to TRUE to display scientific notation.

  • width indicates the minimum width displayed by filling in the blanks at the beginning.

  • justify is the display of strings to the left, right, or center.

Cases

# 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 code above, it produces the following results -

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

Calculates the number of characters in the string - the nchar() function

This function calculates the number of characters in a string that contain spaces.

Grammar

The basic syntax of the nchar() function is -

nchar(x)

The following is a description of the parameters used -

  • x is the vector input.

Cases

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

When we execute the code above, it produces the following results -

[1] 30

Change the case -toupper() and tolower() functions

These functions change the case of the characters of the string.

Grammar

The basic syntax of the toupper() and tolower() functions is -

toupper(x)
tolower(x)

The following is a description of the parameters used -

  • x is the vector input.

Cases

# 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 code above, it produces the following results -

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

Extract part of the string - substring() function

This function extracts parts of the string.

Grammar

The basic syntax of the substring() function is -

substring(x,first,last)

The following is a description of the parameters used -

  • x is the character vector input.

  • The first is the position of the first character to extract.

  • last is the position of the last character to extract.

Cases

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

When we execute the code above, it produces the following results -

[1] "act"