Home > Mobile >  How do I preserve internally-created trailing zeros when exporting a CSV from R?
How do I preserve internally-created trailing zeros when exporting a CSV from R?

Time:02-19

First, this question is very close, but not quite what I need. Allow me to explain:

I am working with a series of data which requires me to tag according to an internal standard in which decimal places are used as separators. A generic example is below:

ItemA  1.6            
ItemB  1.7             
ItemB  1.8
ItemB  1.9
ItemB  1.10

Now, when this data gets exported as a csv file, the 1.10 gets changed to 1.1. We already use 1.1 for something else, and unlike the linked question above, this tagging is done in the program. The data that is read in for processing does not contain the numbers. For reasons that are irritating, the output must be a csv, and I cannot change how the tagging is done. The output csv file goes elsewhere for further processing by someone else. Changing the class of the value does not have any effect, it seems.

Added in from my comment to the first answer, for clarification: I try to tag as 1.10 in R, and it becomes 1.1 instead, mixing with my other 1.1. To avoid this, I have made them as.character('1.10') but that still doesn't help with the exported csv file. At no point is a csv containing '1.10' being read in.

Is there any way to get r to write the 1.10 into the csv? I don't care how involved or "janky" it is.

CodePudding user response:

You can specify colClasses in read.csv.

Let's make a reproducible example:

df <- data.frame(col1 = c("ItemA", "ItemB", "ItemB", "ItemB", "ItemB"), 
                 col2 = c("1.6", "1.7", "1.8", "1.9", "1.10"))

write.csv(df, file = "my.csv", quote = FALSE)

So now my.csv looks like this:

,col1,col2
1,ItemA,1.6
2,ItemB,1.7
3,ItemB,1.8
4,ItemB,1.9
5,ItemB,1.10

And we can reproduce your problem by doing:

read.csv("my.csv", row.names = 1)
#>    col1 col2
#> 1 ItemA  1.6
#> 2 ItemB  1.7
#> 3 ItemB  1.8
#> 4 ItemB  1.9
#> 5 ItemB  1.1

But if we specify colClasses, the problem is resolved:

read.csv("my.csv", row.names = 1, colClasses = c("character", "character"))
#>    col1 col2
#> 1 ItemA  1.6
#> 2 ItemB  1.7
#> 3 ItemB  1.8
#> 4 ItemB  1.9
#> 5 ItemB 1.10
  •  Tags:  
  • r csv
  • Related