Skip to content

data after write() is off by 1 ?

6 messages · Brian Feeny, Duncan Murdoch, Rui Barradas +1 more

#
A followup to my own post, I believe I figured this out, but if I should be doing something different please correct:
This gives me my correctly adjusted values

Brian
On Nov 20, 2012, at 2:30 PM, Brian Feeny wrote:

            
#
On 20/11/2012 2:30 PM, Brian Feeny wrote:
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
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.

Duncan Murdoch
#
Hello,

You are seeing the levels of a factor but saving its values. Internally, 
factors are coded as consecutive integers starting at 1, and that's what 
is saved to file using write.table. To have the levels "0", "1", etc and 
not the corresponding values 1, 2, etc, try

levels(prediction)[prediction]

or

as.integer(levels(prediction)[prediction])


Hope this helps,

Rui Barradas
Em 20-11-2012 19:30, Brian Feeny escreveu:
#
On 20/11/2012 19:46, Duncan Murdoch wrote:
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?.

  
    
#
On 12-11-20 4:35 PM, Prof Brian Ripley wrote:
Yes, I didn't claim otherwise.

Duncan Murdoch