R Strings

R string is written within a pair of the single quote or double quotes. Internally, R stores every string within double quote (" "), even when you create them with a single quote (' ').

Example 1:

> a <- 'Hello World'
> a

Output:

[1] "Hello World"

Example 2:

> a <- "Hello World"
> a

Output:

[1] "Hello World"

String Manipulation

There are so many types of functions available for string manipulation:

paste()

In R paste() function is used to concatenate the strings.  In short, we can say that it is used to combine the strings. It can take one or more number of arguments to be combined together.

Syntax:

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

Arguments:

  • one or more number of arguments (R objects) to be combined
  • sep used to represent any separator between the arguments. It is optional.
  • collapse is used to remove the space in between two strings but not the space within two words of one string.

Example:

p <- "Hello"
q <- 'How'
r <- "are you? "
print(paste(p,q,r))
print(paste(p,q,r, sep = "-"))
print(paste(p,q,r, sep = "", collapse = ""))

Output:

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

format()

Strings and numbers can be formatted to a specific style using the format() function.

Syntax:

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

Arguments:

  • x is the vector input or any R object typically numeric
  • digits total number of digits to be displayed
  • nsmall minimum number of digits to the right of the decimal point.
  • scientific to display scientific notation it should be set to TRUE.
  • width indicates the minimum width to be displayed by padding space in the beginning.
  • justify is used to set the display of the string to left, right or center.

Example:

# Total number of digits displayed. Last digit rounded off.
result <- format(24.123456789, digits = 9)
print(result)
# Display numbers in scientific notation.
result <- format(c(13, 13.14521), scientific = TRUE)
print(result)
# The minimum number of digits to the right of the decimal point.
result <- format(24.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)

Output:

[1] "24.1234568"
[1] "1.300000e+01" "1.314521e+01"
[1] "24.47000"
[1] "6"
[1] "  13.7"
[1] "Hello   "
[1] " Hello  "

nchar()

This function is used to count the number of characters including spaces in a string.

Syntax:

nchar(x)

Arguments:

  • x is the vector input.

Example 1:

result <- nchar("Hello myself Nikita Kesharwani")
print(result)

Output:

[1] 30

Example 2:

x <- c("red","green","blue")
nchar(x)

Output:

[1] 3 5 4

toupper()

This function is used to convert string to its upper case.

Syntax:

toupper(x)

Arguments:

x is the vector input.

Example:

> toupper("Hello! Myself Nikita Kesharwani")

Output:

[1] "HELLO! MYSELF NIKITA KESHARWANI"

tolower()

This function is used to convert string to its lower case.

Syntax:

tolower(x)

Arguments:

x is the vector input.

Example:

> tolower("Hello! Myself Nikita Kesharwani")

Output:

[1] "hello! myself nikita kesharwani"

substring()

This function is used to extract part of the string.

Syntax:

substring(x, first, last)

Arguments:

  • x is the vector input.
  • first is the position of the first character to be extracted.
  • last is the position of the last character to be extracted.

Example:

# Extracts characters from 3rd to 8th position
> r <- substring("Hello World", 3, 8)
> r

Output:

[1] "llo Wo"

replace()

This function is used to replace the values in x with indices given in the list by those given in values.

Syntax:

replace(x, list, values)

Arguments:

  • x is the vector input.
  • list indices
  • values replacement value

Example 1:

> x <- c("Good","Better","Best")
> x <- c("Nidhi","Deep","Nikita")
> y <- replace(x,1,"Naina")
> y

Output:

[1] "Naina"  "Deep"   "Nikita"

Example 2:

> y <- replace(x,c(1,2),c("good","Best"))
> y

Output:

[1] "good"   "Best"   "Nikita"
Reference: https://www.tutorialspoint.com/r/r_strings.htm https://www.w3schools.in/r/strings/



ADVERTISEMENT
ADVERTISEMENT