acc
acc

Reputation: 35

R reading values of numeric field in file wrongly

R is reading the values from a file wrongly. One can check if this statement is true with the following example:

A sample picture/snapshot which explains the problem areas is hereenter image description here

(1) Copy paste the following 10 numbers into a test file (sample.csv)

1000522010609612
1000522010609613
1000522010609614
1000522010609615
1000522010609616
1000522010609617
971000522010609612
1501000522010819466
971000522010943717
1501000522010733490

(2) Read these contents into R using read.csv

X <- read.csv("./test.csv", header=FALSE)

(3) Print the output

print(head(X, n=10), digits=22)

The output I got was

                           V1
1     1000522010609612.000000
2     1000522010609613.000000
3     1000522010609614.000000
4     1000522010609615.000000
5     1000522010609616.000000
6     1000522010609617.000000
7   971000522010609664.000000
8  1501000522010819584.000000
9   971000522010943744.000000
10 1501000522010733568.000000

The problem is that rows 7,8,9,10 are not correct (check the sample 10 numbers that we considered before).

What could be the problem? Is there some setting that I am missing with my R - terminal?

Upvotes: 0

Views: 99

Answers (1)

akrun
akrun

Reputation: 887118

You could try

library(bit64)
x <- read.csv('sample.csv', header=FALSE, colClasses='integer64')
x
#                   V1
#1     1000522010609612
#2     1000522010609613
#3     1000522010609614
#4     1000522010609615
#5     1000522010609616
#6     1000522010609617
#7   971000522010609612
#8  1501000522010819466
#9   971000522010943717
#10 1501000522010733490

If you load the bit64, then you can also try fread from data.table

library(data.table)
x1 <- fread('sample.csv')
x1
#                   V1
#1:    1000522010609612
#2:    1000522010609613
#3:    1000522010609614
#4:    1000522010609615
#5:    1000522010609616
#6:    1000522010609617
#7:  971000522010609612
#8: 1501000522010819466
#9:  971000522010943717
#10: 1501000522010733490

Upvotes: 1

Related Questions