Losing trailing zeros when exporting .CSV from Excel into R

I have a .CSV file containing numeric data, i.e., "2.0", "3.0", etc. for 36 different observations of two different dependent variables. When I use read.csv to import this file, I am losing the trailing zeros, i.e., the values are reformatted to "1", "2", "3", etc. To circumvent this, I have changed my code as follows:

(file = "Ant_Data.csv",header = TRUE, colClasses = c("character", "character", "character", "character"))
This worked in that it preserves the trailing zeroes. But sadly, it throws a snag in my script because, since these are now being read as character values, I can no longer perform any numerical analyses. Is there a way to preserve my trailing zeroes AND numerical fatting when importing my .CSV file into R?

Trailing zeros suchas those in your examples are formatted numbers, which are characters.

If you wish to calculate using them then convert them to numbers using as.numeric().

1 Like

For me, numerical 2.0 is the same as 2. You could change the formatting when displaying the data - but in terms of reading it in, read.csv was doing the right thing

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.