[R] numerical or not?

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Apr 27 00:30:24 CEST 2010


On 26-Apr-10 21:19:51, 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?
> 
> This time I attached the temporary_output.txt:

Thanks for supplying the file, Laetitia. it consists of one row
of variable-names (1600 of them), a first column of row-names
(integers **in quotes** from "1", "2", ... , "26", "27"), and
then 1600 columns each with 27 items of numerical (integer) data.

When read in using

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

the result is dataframe of 1600 columns and 27 rows pof data.
The result of str(dat1) is:

  str(dat1)
  # 'data.frame':   27 obs. of  1600 variables:
  # $ P38    : int  4 4 3 4 4 4 3 4 4 2 ...
  # $ P39    : int  3 4 3 4 4 4 4 4 3 2 ...
  # $ P40    : int  4 2 4 3 4 3 4 4 3 4 ...
  # $ P41    : int  4 3 3 3 4 4 2 3 4 3 ...
  # $ P44    : int  4 3 4 3 4 3 3 4 3 4 ...
  # $ P45    : int  4 3 3 3 2 2 3 3 2 4 ...
  # $ P46    : int  3 3 3 3 4 3 2 3 3 4 ...
  # $ P50    : int  3 4 3 3 3 3 4 3 3 4 ...
  # $ P64    : int  3 3 3 2 4 2 4 4 4 4 ...
  # $ P72    : int  4 4 3 3 4 3 3 2 3 3 ...
  # $ P131   : int  4 3 3 3 3 3 4 4 3 4 ...
  # $ P132   : int  4 3 3 4 3 3 3 3 3 4 ...
  # $ P161   : int  4 3 3 4 3 3 4 3 3 3 ...
  # $ P174   : int  4 3 4 3 3 2 3 4 3 3 ...
  # $ P183   : int  3 2 4 3 4 3 3 4 4 4 ...
  # ....

I have encountered no problem doing numerical calculations
with the data in this dataframe. Example:

  for(i in (1:27)){print(sum(dat[i,]))}
  # [1] 5095
  # [1] 5276
  # [1] 5004
  #     ....
  # [1] 5552
  # [1] 5417
  # [1] 5452

There are no non-numeric data in any row or column.

So it is totally unclear why, when you "want to calculate
things", you should get a statement to the effect that
"x is not numeric". What is "x", by the way? Is it a variable
in the "things" you want to calculate?

The best next step is for you to post an example of a calculation
based on "dat1" (as read in by the above command from the file
"temporary_output.txt" which you supplied) which gives rise to
the message about "not numeric". Then we can take it further.

As things stand, there is nothing visibly wrong with your data file,
so the problem almost certainly lies in the calculations that
you are trying to do.

Ted.

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



More information about the R-help mailing list