简体   繁体   中英

R is turning large numbers to negative random numbers

I am having an issue when importing into R multiple data structures saved as *.mat files by using readMat().

When I read and open the file in R, the content of one single column stored in the data structure changes (apparently) randomly (should be 1504615865460506 and is -1372641510 for example). Also, numbers in the original *.mat file are increasing (1st is 1484649519139343, 2nd is 1484649519142687 etc), while in R corresponding numbers are decreasing (1st is -1372641510, 2nd is -1372633137 etc). No change is occurring on the other variables.

The "wronged" variable is the TimeStamp, a progressive number indicating the exact time a data point was recorded. It is stored into a column, part of a list, part of a even bigger list. It is 'integer' according to class(). I read readMat() documentation, though I did not find anything related. In case the problem was the large number, I set options(digits=20), with no effect.

Any ideas/suggestions will be greatly appreciated!

And I attach my code.

library(R.matlab)
setwd("C:/Path") 
options(digits=20)
temp = list.files(pattern="*.mat") 
list2env(lapply(setNames(temp, make.names(gsub("*.mat$", "", temp))), readMat), envir = .GlobalEnv) 
rm(temp) 
listAll<-list(mget(ls())) #listAll contains all the *.mat files.



listAll[[1]][[3]] #listAll contains N lists == N of *.mat files. 

This is an example of the structure of the 3rd list:

# $data
# , , 1
# 
# [,1]   
# ID          "A6001"
# TimePoint   "10"   
# MainBuffer  List,12
# TimeBuffer  List,12 #TimeBuffer is the Time Stamp. 

TimeBuffer contains 12 lists, corresponding to 12 trials. Each trial has 1 column, which is the one that is unwillingly changed in the process.

# EventBuffer List,12
# Log         List,12
# 
# 
# attr(,"header")
# attr(,"header")$description
# [1] "MATLAB 5.0 MAT-file, Platform: MACI64, Created on: Wed Sep 20 16:03:45 2017                                         "
# 
# attr(,"header")$version
# [1] "5"
# 
# attr(,"header")$endian
# [1] "little"

listAll[[1]][[3]][[1]][[4]][[3]][[1]][1,][1]

#[1] 668725504 (and should be 1480500650907453)

should be 1504615865460506 and is -1372641510 for example

Looks like an overflow error.

From help(integer) in R:

Note that current implementations of R use 32-bit integers for integer vectors, so the range of representable integers is restricted to about +/-2*10^9: 'double's can hold much larger integers exactly.

So, you'll need to use a larger type such as double for the values in question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM