How to use the str_pad function in R?

How to use the str_pad function in R?

The str_pad function in R is used to add padding characters to a string, either at the beginning or at the end. This can be useful for formatting strings in a specific way, such as adding leading zeros to numbers. To use the str_pad function, first specify the string that needs to be padded and then specify the desired length of the final string. Next, specify the padding character and the side where it should be added (either “left” or “right”). The function will then add the necessary padding characters to the string and return the updated string. The str_pad function is a useful tool for manipulating and formatting strings in R.

Use str_pad in R (With Examples)


The str_pad() function from the package in R can be used to pad characters to a string.

This function uses the following syntax:

str_pad(string, width, side = c(“left”, “right”, “both”), pad = ” “)

where:

  • string: Character vector
  • width: Minimum width of padded strings
  • side: Side to add padding character (Default is left)
  • pad: Character to use for padding (Default is space)

The following examples show how to use this function in practice

Example 1: Pad String with Spaces

The following code shows how to use the str_pad() function to pad the left side of a string with spaces until the string has 10 total characters:

library(stringr)

#create string
my_string <- "Rhino"

#pad string to length of 10
str_pad(my_string, width=10)

[1] "     Rhino"

Notice that five spaces have been added to the left side of the string so that the string has a total length of 10.

Use the side argument to instead pad the right side of the string:

library(stringr)

#create string
my_string <- "Rhino"

#pad string to length of 10
str_pad(my_string, width=10, side="right")
[1] "Rhino     "

Example 2: Pad String with Specific Character

The following code shows how to use the str_pad() function to pad the left side of a string with underscores until the string has 10 total characters:

library(stringr)

#create string
my_string <- "Rhino"

#pad string to length of 10 using underscores
str_pad(my_string, width=10, pad="_")
[1] "_____Rhino"

Notice that five underscores have been added to the left side of the string so that the string has a total length of 10.

Example 3: Pad String with Specific Number of Characters

library(stringr)

#create string
my_string <- "Rhino"

#pad string with 5 A's
str_pad(my_string, width=nchar(my_string)+5, pad="A")
[1] "AAAAARhino"

Notice that five A’s have been padded to the left side of the string.

Cite this article

stats writer (2024). How to use the str_pad function in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-use-the-str_pad-function-in-r/

stats writer. "How to use the str_pad function in R?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-to-use-the-str_pad-function-in-r/.

stats writer. "How to use the str_pad function in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-to-use-the-str_pad-function-in-r/.

stats writer (2024) 'How to use the str_pad function in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-use-the-str_pad-function-in-r/.

[1] stats writer, "How to use the str_pad function in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How to use the str_pad function in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top