Say that I have a dataframe with a column that has values that are either three or four numbers in length. What I want to do, is that whenever length = 3, I want to insert a 0 (zero) at the start of the number, for all values in the column, like this:
320 -> 0320
0672 -> 0672
120 -> 0120
Is there any way to do this? I thought of some kind of for-loop for this, but that may be unnecessarily complicated... (I am quite new to programming in general).
for ( i in dataset$col_name){
if (nchar(dataset$col_name == 3)){
dataset$col_name <- 0 + dataset$col_name
} else {
dataset$col_name <- dataset$col_name
}
}
Thanks for help!