[R] data after write() is off by 1 ?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Nov 20 22:38:52 CET 2012


On 12-11-20 4:35 PM, Prof Brian Ripley wrote:
> On 20/11/2012 19:46, Duncan Murdoch wrote:
>> On 20/11/2012 2:30 PM, Brian Feeny wrote:
>>> I am new to R, so I am sure I am making a simple mistake.  I am
>>> including complete information in hopes
>>> someone can help me.
>>>
>>> Basically my data in R looks good, I write it to a file, and every
>>> value is off by 1.
>>>
>>> Here is my flow:
>>>
>>>> str(prediction)
>>>    Factor w/ 10 levels "0","1","2","3",..: 3 1 10 10 4 8 1 4 1 4 ...
>>>    - attr(*, "names")= chr [1:28000] "1" "2" "3" "4" ...
>>
>> You have a factor, not numerical data.  Apparently write() is writing
>> out the factor values (index into the levels) rather than their string
>> representation.  (I've never used write().  Normally would use cat() or
>> write.csv() or something related to write data
>
> But as the help page says
>
>        ‘write’ is a wrapper for ‘cat’, which gives further details on the
>        format used.
>
> and cat() does treat a factor as an integer vector:
>
>        Currently only atomic vectors and names are handled, together with
>        ‘NULL’ and other zero-length objects (which produce no output).
>        Character strings are output ‘as is’ (unlike ‘print.default’ which
>        escapes non-printable characters and backslash - use
>        ‘encodeString’ if you want to output encoded strings using ‘cat’).
>        Other types of R object should be converted (e.g. by
>        ‘as.character’ or ‘format’) before being passed to ‘cat’.
>
>
>> to a file for reading outside of R. )  write.csv() will write out the
>> strings, by default in quotes, but there are lots of arguments
>> to control the formatting.

Yes, I didn't claim otherwise.

Duncan Murdoch




More information about the R-help mailing list