Add Leading Zeros to the Elements of a Vector in R Programming - Using paste0() and sprintf() Function Last Updated : 15 Jul, 2025 Comments Improve Suggest changes Like Article Like Report paste0() and sprintf() functions in R Language can also be used to add leading zeros to each element of a vector passed to it as argument. Syntax: paste0("0", vec) or sprintf("%0d", vec)Parameters: paste0: It will add zeros to vector sprintf: To format a vector(adding zeros) vec: Original vector dataReturns: Vectors by addition of leading zeros Example 1: r # R Program to add leading zeros # Create example vector vec <- c(375, 21, 1, 7, 0) vec # Add leading zeros vec_0 <- paste0("0", vec) vec_0 Output : [1] 375 21 1 7 0 [1] "0375" "021" "01" "07" "00" Example 2: r # R Program to add leading zeros # Create example vector vec <- seq(5) # Add leading zeros sprintf("sequence_%03d", vec) Output : [1] "sequence_001" "sequence_002" "sequence_003" "sequence_004" "sequence_005" Comment More infoAdvertise with us Next Article Concatenation of Elements without Separator in R Programming - paste0() Function K kaurbal1698 Follow Improve Article Tags : R Language R Vector-Function Similar Reads Concatenation of Elements without Separator in R Programming - paste0() Function paste0() function in R Language is used to concatenate all elements without separator. Syntax: paste0(..., collapse = NULL) Parameters: ...: one or more R objects, to be converted to character vectors. collapse: an optional character string to separate the results. Example 1: Python3 # R program to 1 min read Concatenation of Elements without Separator in R Programming - paste0() Function paste0() function in R Language is used to concatenate all elements without separator. Syntax: paste0(..., collapse = NULL) Parameters: ...: one or more R objects, to be converted to character vectors. collapse: an optional character string to separate the results. Example 1: Python3 # R program to 1 min read Convert elements of a Vector to Strings in R Language - toString() Function toString() function in R Programming Language is used to produce a single character string describing an R object. Syntax: toString(x, width = NULL) Parameters:Â x: R objectwidth: Suggestion for the maximum field width. Values of NULL or 0 indicate no maximum. The minimum value accepted is 6 and sma 2 min read Convert elements of a Vector to Strings in R Language - toString() Function toString() function in R Programming Language is used to produce a single character string describing an R object. Syntax: toString(x, width = NULL) Parameters:Â x: R objectwidth: Suggestion for the maximum field width. Values of NULL or 0 indicate no maximum. The minimum value accepted is 6 and sma 2 min read Creating a Vector of sequenced elements in R Programming - seq() Function In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct 2 min read Creating a Vector of sequenced elements in R Programming - seq() Function In This article, we will discuss how we Create a Vector of sequenced elements in R Programming Language using seq() Function. What are sequenced elements?Sequenced elements mean things that are placed in a particular order, one after another. This concept is often used in various fields. seq() Funct 2 min read Like