François M.
François M.

Reputation: 4278

Pad strings in a column of a data.frame

I have a data.frame X, with a column A filled with chr, most of them are of nchar = 5, but some are of nchar=4. I want to put a 0 in front of those.

I would do it with the following kind-of-pseudo-code :

foreach( element_of_X$A as a ){  # this line is pseudo-code for Idk how to do it in R
    if(nchar(a) < 5){            # I think these lines are valid
        paste0(0,a)              # I think these lines are valid
    }
}

(Obviously I come from PHP). How can I do that in clean R code ? (That, or a more efficient solution)

Thanks

Upvotes: 0

Views: 2680

Answers (5)

steveb
steveb

Reputation: 5532

If you use dplyr and stringr you could do the following

library(dplyr)
library(stringr)

## Assuming "element_of_X" has element 'A'
element_of_X <-
    element_of_X %>%
    mutate(A = str_pad(A, 5, side = 'left', pad = '0'))

Edit

Or perhaps more simply, as suggested in the comments:

element_of_X$A <- str_pad(element_of_X$A, 5, side = 'left', pad = '0')

Upvotes: 2

Luke
Luke

Reputation: 1673

library(stringr)
x$A=str_pad(x$A, 5, pad = "0")

Upvotes: 0

C8H10N4O2
C8H10N4O2

Reputation: 19025

Actually sprintf didn't work for me, so if you don't mind a common dependency:

#reproducible example -- this happens with zip codes sometimes
X <- data.frame(A = c('10002','8540','BIRD'), stringsAsFactors=FALSE)

# X$A <- sprintf('%05s',X$A) didn't work for me
# Note in ?sprintf: 0: For numbers, pad to the field width with leading zeros. 
# For characters, this zero-pads on some platforms and is ignored on others.

library('stringr')
X$A <- str_pad(X$A, width=5, side='left', pad='0')
X
#      A
#1 10002
#2 08540
#3 0BIRD

or, if you prefer a base solution, the following is equivalent:

X$A <- ifelse(nchar(X$A) < 5, paste(c(rep("0",5-nchar(X$A)), X$A), collapse=""), X$A)

(note this works on strings of length 4 or less, not just 4)

Upvotes: 6

Wyldsoul
Wyldsoul

Reputation: 1553

This should do the trick:

X$A <- ifelse(nchar(X$A) < 5, paste("0", X$A, sep=""), X$A)

Upvotes: 2

Gopala
Gopala

Reputation: 10483

Try something like this (assuming data frame name and column name are right):

element_of_X$a <- with(element_of_X , ifelse(nchar(a) == 4, paste('0', a, sep = ''), a)

Upvotes: 1

Related Questions