[R] Reading Numeric Data -- Trivial Question

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri May 20 22:49:33 CEST 2005


Alex K wrote:

> On 5/20/05, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
> 
>>Alex K wrote:
>>
>>
>>>Hello,
>>>
>>>   I am very new to R, and this is certainly and uber-newby question:
>>>
>>>   I am trying to read a vector of numeric data that contains the log
>>>of daily DJI returns and simply plot a histogram of it.
>>>
>>>   The data I have is in a text file format, on each line a number
>>>represents the log of the returns of one day.
>>>
>>>   I have problems in reading this in a vector numeric format.
>>>
>>>If I try
>>>
>>>retr<- as.numeric(read.table("logDJI.TXT", header=FALSE, sep="", dec="." ));
>>
>>
>>read.table() returns a data.frame, as the docs point out, but as.numeric
>>expects a vector.
>>
>>Either extract the first (and only?) column or read the data using
>>scan(), if there is really only one column.
>>
>>Uwe Ligges
> 
> 
>   Hi Uwe, thank you for the reply.
> 
>   How do I extract only the colum?
> 
> The text file looks like:
> "-0.000948343416257784
> 0.00181733586025109
> 0
> 0.0108130251220595"
> 
> except is has about 2200 entries (numbers) instead of just four as above.

Please, DO read the posting guide (cited at the bottom of each message)!
There is a manual "An Introduction to R". Please read it!
There is a manual on Data Import and Export. Please read it!

Really, why don't you read up on the hints you got in my former message? 
Why do you waste bandwith and our time with an unnecessary followup?

As I said, extract the first column of your data.frame. Hint: there are 
index operators such as in MyDataFrame[,1] or MyDataFrame[[1]].
Or use scan(). What's so difficult to read ?scan and try to apply it?

Uwe Ligges





> Thank you again,
> Alex
> 
> 
> 
> 
>>
>>>I get:
>>>" Error in as.double.default(read.table("logDJIm.TXT", header = FALSE,
>>>sep = "",  :
>>>        (list) object cannot be coerced to 'double'"
>>>
>>>and when I try to plot:
>>>
>>> plot(density(retr, width=.004), type="l", xlab="y", ylab="pdf");
>>>
>>>I get:
>>>
>>>"Error in density(retr, width = 0.004) : argument 'x' must be numeric"
>>>
>>>  If I try:
>>>
>>>retr<- as.data.frame(read.table("logDJI.TXT", header=FALSE, sep="", dec="." ));
>>>
>>>I get no reading or conversion error, but I get the same error when I
>>>try to plot as above.
>>>
>>>Can anyone help with this?
>>>
>>>Thank you in advance,
>>>Alex
>>>
>>>______________________________________________
>>>R-help at stat.math.ethz.ch mailing list
>>>https://stat.ethz.ch/mailman/listinfo/r-help
>>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>>
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list