[R] numerical or not?

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Apr 26 00:02:41 CEST 2010


On 25-Apr-10 21:20:44, Laetitia Schmid wrote:
> Hi,
> I've had a little problem for several weeks now. It is annoying and  
> therefore I will ask for help:
> When I write a script with several iterations, I make it write out a  
> text file to save the data during the run. For example I write:
> if (i %% 25) write.table(output,"temporary_output.txt")
> Later on, when I read in this output and want to calculate things, R  
> complains that x is not numeric.
> I read it in with the following command:
> dat1<-read.table("temporary_output.txt",header=TRUE).
> It seems that R is saving my temporary output as a list.
> What is wrong here? And how can I do it better?
> 
> Thanks.
> Laetitia

Presumably your "output" which you write is already a matrix-like
or dataframe-like object (i.e. with columns all of the same length,
so that it can be coerced to a dataframe -- see ?write.table ).
In that case the writing should go smoothly.

Then the problem may lie in the contents of "temporary_output.txt".
Have you opened the file using, say, a suitable text-editor
(depending on your operating system -- in Linux it's simplest
to use 'less' to page through it)

You may then be able to see if any of the columns are non-numeric
(e.g. have they come out with quotation-marks so they would be
read in as character variables; or if some entries are non-numeric,
in which case the whole column would be converted).

For example, if you had in "temporary_output.txt":

  1.1  1.2  1.3  1.4
  2.1  2.2  2.3  2.4
  3.1    A  3.3  3.4

then column 2 would be a character variable. Note that the result
of read.table() is a dataframe which is a special kind of list.

Note also from ~write.table that the default for the paramater
"quote" is TRUE, in which case "any character or factor columns
will be surrounded by double quotes." So then if any of the
variables in "output" are factors, then they will get quotes,
and when read back in they will be factors again and therefore
non-numeric.

It could also be worth having a a look at str(dat1) after your
command

  dat1<-read.table("temporary_output.txt",header=TRUE)

Check things out, and let us know what you see!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 25-Apr-10                                       Time: 23:02:35
------------------------------ XFMail ------------------------------



More information about the R-help mailing list