George Coyle
2010-Sep-29 17:46 UTC
[R] Converting a line by line program into an array to perform summary stats
I am trying to turn several lines of information into a variable. I used the filx function to input my file then the readlines to qualify what I want. Essentially I have data in a file every 10 minutes through a day for several years down a column: date time value 9/28/10 02:00 13 9/28/10 02:10 15 9/28/10 02:20 12 9/28/10 02:30 11 etc. I then use if statements to segment the day into various portions (2:00 to 2:30 for example) and find the average or maximum value for the time. Next I compare this value to another time of the day to see if the price was reached/broken. For example the 2:00 to 2:30 low from above is 11 at 2:30. I would then check the 8:00 to 13:00 time period to see if the 2:00 to 2:30 low occured from 8:00 to 13:00. If it did occur I would call this day a qualifier and check the time 2:30 price occured in the 8:00 to 13:00 period until the price at 13:00. So another example if 2:30 was 11, 8:00 was 13, 8:45 was 11, and 13:00 was 20 I would say this day qualified and the price from the am was first acheived at 8:45 in the 8:00 to 13:00 period and the change from that price to the 13:00 period was 20-11=+9. This example was all background to my problem. Using the filx/readlines methods I wind up with output for the days which qualify, but when I try to create an array variable using the total of each line for a given column (aka summarizing every day that my given criteria were met), something about having used the line by line method doesn't allow me to create an array variable. It only gives me the last value. The variables I declared to fidn the qualifying days are specific to each day vs across all days that qualified. Is it possible to convert this line by line data into an array wih some R code I don't know? I considered converting the data example above into a matrix, but then I run into tricky problems of shortened days and days missing data (the code I used for the readlines method enables me to specify the new days without this problem...I don't have to fill in blanks it just changes days when it hits a new day). Given how specific this problem is I can't find much on the R help logs. Perhaps outputting the line by line output to a file, then reinputting it using a read.csv method would enable me to achieve my desired result but then I have the laborious task of dumping into excel, converting file types, saving as csv, reuploading to R, writing a separate program, etc. Is there an easy fix to my problem? Thanks [[alternative HTML version deleted]]
Dieter Menne
2010-Sep-29 18:29 UTC
[R] Converting a line by line program into an array to perform summary stats
George Coyle wrote:> > I am trying to turn several lines of information into a variable. I used > the filx function to input my file then the readlines to qualify what I > want. Essentially I have data in a file every 10 minutes through a day > for > several years down a column: > > date time value > 9/28/10 02:00 13 > 9/28/10 02:10 15 > 9/28/10 02:20 12 > 9/28/10 02:30 11 > etc. > > I then use if statements to segment the day into various portions (2:00 to > 2:30 for example) and find the average or maximum value for the time. > Next > ..... >George, it is very difficult to understand what you want, so let' try to chop it down. Let's say you want to find the best way to do some time of processing on what generally called a time series. First, reading this into a matrix will not work, because you have three different column types. The standard workhorse for this type of data is a dataframe, and read.csv gives a data frame. Next, note that your structure is even simpler: you have a time series, probably not an regular one, because I assume your stock quotes are not available all the time with equal intervals, but will stop at night. So the general idea would be:>> read in a data frame with read.csv >> convert to a time series. See package zoo for many examples >> do the processing (which I do not understand) on the time seriesDieter -- View this message in context: http://r.789695.n4.nabble.com/Converting-a-line-by-line-program-into-an-array-to-perform-summary-stats-tp2719489p2719542.html Sent from the R help mailing list archive at Nabble.com.