[R] iterating over a data frame the R way?

William Dunlap wdunlap at tibco.com
Fri Jan 1 00:46:40 CET 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Gabor Grothendieck
> Sent: Thursday, December 31, 2009 3:15 PM
> To: donahchoo at me.com
> Cc: r-help at r-project.org
> Subject: Re: [R] iterating over a data frame the R way?
> 
> This iterates functions f over successive rows of mytable:
> 
> apply(mytable, 1, f)

Unless all columns of the data.frame mytable are numeric
apply() is not usually a good way to go, as it converts the
data.frame to a matrix, coercing all the columns to a common
type, often character.  E.g.,
 > mytable <- data.frame(
   start_time=seq(as.POSIXct("2009-12-31 15:27"), by="hours", len=12),
   end_time=seq(as.POSIXct("2009-12-31 15:27"), by="days", len=12))
 > apply(mytable, 1, function(row)row['end_time']-row['start_time'])
 Error in row["end_time"] - row["start_time"] : 
  non-numeric argument to binary operator
Try apply(mytable,1,class) to see that all rows are character
data.

I prefer columnwise operations like the following when they
are feasible.  They directly express what you want to do
and are quick to execute.

 > mytable$duration <- mytable$end_time - mytable$start_time
 > mytable
             start_time            end_time    duration
 1  2009-12-31 15:27:00 2009-12-31 15:27:00      0 secs
 2  2009-12-31 16:27:00 2010-01-01 15:27:00  82800 secs
 ...
 11 2010-01-01 01:27:00 2010-01-10 15:27:00 828000 secs
 12 2010-01-01 02:27:00 2010-01-11 15:27:00 910800 secs

The expression
 > mytable <- within(mytable, duration <- end_time - start_time)
is syntactic sugar for the one I gave above.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> or if you want to use SQL on R data frames then see sqldf
> http://sqldf.googlecode.com
> 
> On Thu, Dec 31, 2009 at 5:52 PM,  <donahchoo at me.com> wrote:
> > In this specific case I'm going to do another select, from 
> a different table
> > with the where clause being based on the start_time and end_time.
> >
> > select * from table_name where time >= start_time and time 
> <= end_time;
> >
> >
> > On Dec 31, 2009, at 3:57 PM, John Kane wrote:
> >
> >> Uh what do you want to do to it/them?
> >>
> >> Here are a couple of R-type commands on a data.frame.
> >>
> >> mydata  <- data.frame(vec1 =  seq(19,109, by=10),
> >> vec2 =seq(30,120, by=10))
> >>
> >> mydata[,1]+mydata[,2]
> >> apply(mydata, 2, mean)
> >>
> >>
> >>
> >> --- On Thu, 12/31/09, donahchoo at me.com <donahchoo at me.com> wrote:
> >>
> >>> From: donahchoo at me.com <donahchoo at me.com>
> >>> Subject: [R] iterating over a data frame the R way?
> >>> To: r-help at r-project.org
> >>> Received: Thursday, December 31, 2009, 2:44 PM
> >>> Hi,
> >>>
> >>> I have a data frame that was create by issuing a select
> >>> against my sqlite database.  I want to get each row
> >>> from the data frame and use the each of the column
> >>> values.  The data frame looks like this:
> >>>
> >>> start_time    end_time
> >>> 09:30:00      10:00:00
> >>> 10:00:01      10:30:00
> >>> etc
> >>>
> >>> Can a point me to a tutorial/example of doing this?  I
> >>> the other programming languages I'm familiar with I would
> >>> just loop over the frame and access the elements, but I
> >>> believe that's not the R way and can't find examples of
> >>> doing this.
> >>>
> >>> Thanks!
> >>>
> >>> ______________________________________________
> >>> R-help at r-project.org
> >>> mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide
> >>> http://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained,
> >>> reproducible code.
> >>>
> >>
> >>
> >>     
> __________________________________________________________________
> >> Make your browsing faster, safer, and easier with the new Internet
> >> Explorer® 8. Optimized for Yahoo! Get it Now for Free! at
> >> http://downloads.yahoo.com/ca/internetexplorer/
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list