Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R not importing csv file correctly

Tags:

import

r

csv

I have a strange problem with R. It does not import a csv file correctly that I am exporting from Excel. I have the following csv file (I checked that the text format was the same as the cell values in Excel):

REGION;TYPE;CODE;BILL
A;X;871685920001760387;003007614504
B;Y ;871685920001765726;003007638434
C;Z;871685920001804326;003211001858

The above are the contents of my csv file. I saved it as "Example.csv". Now I want to import this file into R:

Ex <- read.csv2("Example.csv", header = TRUE, sep = ";")

Now, I specifically want to check that the CODE column matches, for I need these values to compare them against some files that I have stored elsewhere. However, when I compare these files to the tekst file (and the cell values in Excel), using options(digits = 19);

Ex$CODE
[1] 871685920001760384 871685920001765760 871685920001804288

As you can see, these values do not match at all! Trying as.character() gives the same results:

as.character(Ex$CODE)
[1] "871685920001760384" "871685920001765760" "871685920001804288"

Does anyone know how to fix this problem? I also tried stringsAsFactors = FALSE which did not work.

Thanks in advance!

like image 663
Tim.Lucas Avatar asked Dec 11 '22 13:12

Tim.Lucas


1 Answers

You can read them all in as characters by setting colClasses.

 > Ex = read.table("Example.csv", sep  = ";", header = TRUE, colClasses = "character")
 > Ex
   REGION TYPE               CODE         BILL
 1      A    X 871685920001760387 003007614504
 2      B   Y  871685920001765726 003007638434
 3      C    Z 871685920001804326 003211001858
!> sapply(Ex, class)
      REGION        TYPE        CODE        BILL
 "character" "character" "character" "character"
like image 127
Jake Burkhead Avatar answered Jan 29 '23 09:01

Jake Burkhead