[R] Issue with write.table and read.table : I'm not getting out what I put in

Peter Ehlers ehlers at ucalgary.ca
Mon Jul 5 19:46:45 CEST 2010


On 2010-07-05 11:30, David Winsemius wrote:
>
> On Jul 5, 2010, at 11:49 AM, Irina wrote:
>
>> Hello,
>>
>> I am trying to save a large matrix of values in a file. My problem is
>> that I am
>> writing
>> write.table(allpos,'control_chr1.txt', dec=".")
>> and then I want to check it with
>> test2=read.table('control_chr1.txt')
>> sum(test2[,2]==allpos[,2])
>>
>> This last number is lower than the length of the test2[,2] vector.
>> This is
>> really annoying me because I can't figure out why I don't get out the
>> same thing
>> that I put in.
>>
> Many potential problems could underly getting FALSE for an "==" test.
> One might be FAQ 7.31. Another might be encoding or locale issues
> related to the decimal separator. Why not post a reproducible example?
>

David's advice is spot-on. (As is Jim's: using save/load is better.)

I have no problem replicating your 'problem' with
random data, showing once again the futility of
using "==" in situations such as this. Try instead:

  all.equal(test2, allpos, check.attributes = FALSE)

(why the check.attributes argument may be needed
is left as an exercise)

   -Peter Ehlers



More information about the R-help mailing list