Skip to content

need more help.... Re: iterating over a data frame the R way?

4 messages · donahchoo at me.com, Gabor Grothendieck, David Winsemius

#
Okay, I don't know if I'm blocking because of my other programming  
language experience of if I'm just being dense.

I have a data.frame with high,open,low,last,day, and start_time time  
columns.  What I want to do is get all the rows with the same day and  
process each of those columns.  If I was able to run a query against  
the data.frame I would be want something like:

result = all the rows where the day is equal to 2007-12-05

Then I would want get max(result["high"]), min(result["low"])

How do I do this?  I tired sqldf, but its complaining about  
libtcl0.5.8 not being found.  I'm on OS X 10.5.8 and haven't yet tired  
to install a new version of tcl in /usr/local.

Here's the dataframe:

-------------- next part --------------
#
Hum.  data.frame was left off.  Trying again...

-------------- next part --------------
On Jan 1, 2010, at 10:20 AM, donahchoo at me.com wrote:

            
#
Reinstall R but this time choose the Mac version that includes tcl.
You shouldn't have to separately install it.
On Fri, Jan 1, 2010 at 11:20 AM, <donahchoo at me.com> wrote:
For sqldf, unlike other platforms where all versions of R include tcl,
on the Mac there are versions of R with and without tcl so you need to
reinstall R using a version that includes it.

However, in this case since you are dealing specifically with
open/high/low/close series, the xts package already has specific
facilities:

library(xts)
library(chron)

# assume object in attachment to original post is called d.
# Convert it to xts object and set columns to OHLC names it understands.
x <- xts(d[1:4], as.chron(paste(d$day, d$start_time)))
colnames(x) <- c("High", "Open", "Low", "Close")

xday <- to.daily(x)
head(xday)
#
On Jan 1, 2010, at 11:20 AM, donahchoo at me.com wrote:

            
The usual method of doing that sort of operation the application of a  
function within categories using tapply. See the examples in:

?tapply
Nope... nothing came through. Read the posting guide for the proper  
way to either include r objects in an email or to attache txt files.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT