lundi 25 mai 2020

How to add a character to a string, based on the number of characters in R? [duplicate]

In R, I'm looking for a way to add a specific character to all values in a column of a data frame based on the number of characters. Put it simply: if cell has 4 characters, do nothing. If a cell has 3 characters, add a 0 in front of every cell value. Every cell value has either 3 or 4 characters.

id <- c(1000, 2000, 300, 400, 5000, 600)
df <- data.frame(id)


#Desired Result (df)
#id         
#1000         
#2000      
#0300
#0400
#5000
#0600

Aucun commentaire:

Enregistrer un commentaire