0

I have a data.frame that contains a column with strings of numbers

 complete_code iris
1     774680203 0203
2     774950102 0102
3     772800000 0000
4     774680206 0206
5     770140104 0104
6     771230000 0000

I want to save it as a .csv file with

write_csv

but when I load it again, I get

> read_csv("file.csv", col_names = TRUE, cols (complete_code = col_character(), iris = col_character)

complete_code  iris
          <dbl> <dbl>
1     774680203   203
2     774950102   102
3     772800000     0
4     774680206   206
5     770140104   104
6     771230000     0

I am therefore losing the first "0" on my string of numbers. I tried using the col_types arguments but can't seem to keep all the "0" in my data set.

Do you have a method that could help me? Thanks

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
PHNM
  • 25
  • 6
  • 1
    You may find [this](https://stackoverflow.com/a/5816779/10852113) post useful. – Dion Groothof Jan 03 '22 at 16:55
  • 1
    I cannot reproduce the error. If I read your data calling it `x` and make sure that the leading 0s are present, then use `write_csv(x, "test.csv")` and read it in again using the default `y <- read_csv("test.csv")`, then `y` is just the same as `x`. Please show the code you are using to write the CSV file, and make extra sure that you didn't lose the leading 0s **before** writing the CSV. – Gregor Thomas Jan 03 '22 at 17:04
  • Try, `read.csv('file.csv', colClasses = c(iris = 'character'))` – Onyambu Jan 03 '22 at 17:25
  • 1
    You are missing a pair of parentheses after the second `col_character`. Does fixing that help? – dash2 Jan 03 '22 at 17:28

0 Answers0