String Operations in R Programming Language


String Operations in R Programming Language

Hello Friends,

In our previous chapter we discussed about the Math built in functions which are deals with the numeric data, but what about the strings? Today we will discuss about the operations which we can do with the strings in R programming. As we all knows that string is a  alphabetically written a character, a word, a line or a full paragraph. So today we will learn how to deal with them with predefined string functions in R programming language. In R any value whether its numeric or character,, written within the pair of single quotes or double quotes are treated as a String value.


> a <- “This is a String”


[1] “This is a String”

So lets get started.

String Manipulation

paste() function:

This function is generally use to combined two or more strings together. It has three parameters:

  • number of arguments to combined.(Required)
  • sep: separator between arguments.(optional)
  • collapse: to eliminate the spaces between arguments.


> a <- “Once”

> b <- “Upon”

> c <- “a time”

> print(paste(a, b, c))

[1] “Once Upon a time“

> print(paste(a,b,c,sep = “/”))

[1] “Opnce/Upon/a time”

> print(paste(a,b,c,collapse=””))

[1] ”OpnceUpona time”

format() function:

Its use to give specific styles to number or strings.


# Total number of digits displayed. Last digit rounded off.
> a <- format(23.123456789, digits = 9)
> print(a)
[1] "23.1234568"

# Display numbers in scientific notation.
> a <- format(c(6, 13.14521), scientific = TRUE)
> print(a)
[1] "6.000000e+00" "1.314521e+01"

# The minimum number of digits to the right of the decimal point.
> a <- format(23.47, nsmall = 5)
> print(a)
[1] "23.47000"

# Format treats everything as a string.
> a <- format(6)
> print(a)
[1] "6"

# Numbers are padded with blank in the beginning for width.
> a <- format(13.7, width = 6)
> print(a)
[1] "  13.7"

# Left justify strings.
> a <- format("Hello", width = 8, justify = "l")
> print(a)
[1] "Hello   "

# Justify string with center.
> a <- format("Hello", width = 8, justify = "c")
> print(a)
[1] " Hello  "

nchar() function:

To count the number of characters (included space), we use nchar() function.


> a <- nchar(“Hello World!”)

> print(a)

[1] 12

toupper() and tolower() function:

To change the character case of the string we use these functions. To change them to upper case we use toupper() and to change them to lower case we use tolower() function.


> a <- toupper(“change this to upper”)

> b <- tolower(“CHANGE THIS TO LOWER”)

> print(a)

> print(b)


[1] change this to lower

substring() function:

This function extracts parts of a String. The parameters are :

  • A character vector input.
  • A number for the first character to be extracted.
  • A number for the last character to be extracted.


> a <- substring(“WallStreet”, 2, 5)



strsplit() function:

This function will split the string by given value by user. This function will remove the value from string and print the rest part of the string by split way.


> a<- “Hello This is a Long String”

> strsplit(a, ‘o’)

[1] “Hell” “This is a L” “ng String”

grep() & grepl() function:

These functions are use to find the patter in a string whether present or not. The grepl will return the value in boolean form and grep will return value in numeric form.


> a <- “This is a String”

#If character present in String returns TRUE

> grepl (“is”, a)

[1] True

#If character is not present then returns FALSE

> grepl (“who”, a)

[1] False

> a <- c(“a”, “b”, “c”)

>grep(“a”, a)

[1] 1

>grep(“b”, a)

[1] 2

>grep(“c”, a)

[1] 3

#if string not found return will be

>grep(“d”, a)

[1] integer(0)

sprintf() function:

This function can print strings with variables in them. This function can replaces the variable names with their values.


> a <- 5L

> b <- “Amar”

> c <- “company”

> sprintf(“%s Working in a %s and earn %d dollars per hour.”)

[1] “Amar Working in a company and earn 5 dollars per hour”

cat() function:

To combined all input objects into a single character vector, we use cat() function. It can also create, edit, or append a file to save the output.


> cat(“Hello”, “This”, “is”, “MTHSCHOOLS.TECH” sep=”-”)


sub() function:

This function will replaces the subset of string which is requested by user.


> a <- “Hello My Name is MTHSCHOOLS”

> sub(“My Name is”, “I am”, a)

[1] “Hello I am MTHSCHOOLS”


Before finishing this article we just wanted to clarify that R has a wide variety of functions that can manipulate any kind of data. Here we just learnt about a few R functions that help manipulate the strings. Just keep learn and find out more about the string manipulation. We will keep update the new functions in our article.

Hope you understand the concept of the string manipulation and some built in functions of string. Feel free to leave your comments below.

Read More:

Thank you

No comments:

Post a comment