0

is there any option in R to change format of entire column? I have a number containing 13 digits and sometimes is starts with 0 and when I am reading dataframe these zeros are missed. I would like to use something like excel formatting and set up column for 13 digits.

I probably should use something like my.dataframe[,my_column] <-

Regards

RafMil
  • 138
  • 2
  • 2
  • 15

1 Answers1

1

If you just want to add leading zeroes to a column, you can use the str_pad command from the stringr package:

library(stringr)
digit <- 123456789
str_pad(digit, width = 13, side = "left", pad = "0")
[1] "0000123456789"

Also this might help: Format number as fixed width, with leading zeros

Cettt
  • 11,460
  • 7
  • 35
  • 58
  • How can in use this in one column in dataframe? Command like str_pad(mydf[[2]],width = 13, side = "left", pad = "0") show proper result on console but is not changing anything in a data frame. – RafMil Apr 04 '18 at 08:18
  • 1
    it shows you the result because you are asking for the result. If you want to assign the result to something you have to tell R: mydf$newcolumn <- str_pad(mydf[[2]],width = 13, side = "left", pad = "0") – Cettt Apr 04 '18 at 09:35
  • Thanks. Now everything is fine :) – RafMil Apr 04 '18 at 11:39