[R] Comparing dates in dataframes

James Rome jamesrome at gmail.com
Sat Jan 16 22:14:36 CET 2010


I have two data frames. One (arr) has all arrivals to an airport for a
year, and the other (gw) has the dates and quarter hour of the day when
the weather is good. arr has a Date and quarter hour column.

>names(arr)
 [1] "Date"     "weekday"      "hour"         "month"        "minute"     
 [6] "quarter"      "ICAO"         "Flight"       "AircraftType"
"Tail"       
[11] "Arrived"      "STA"          "Runway"       "FromTo"      
"Delay"      
[16] "Operator"     "gw" 

I added the gw column to arr and initialized it to all FALSE

>names(gw)
 [1] "Date"           "minute"         "hour"           "quarter"      
 [5] "Efficiency.Val" "Weekly.Avg"     "Arrival.Val"    "Weekly.Avg.1" 
 [9] "Departure.Val"  "Weekly.Avg.2"   "Num.of.Hold"    "Runway"       
[13] "Weather" 

First point of confusion:
>gw[1,1]
[1] 1/1/09
353 Levels: 1/1/09 1/1/10 1/10/09 1/10/10 1/11/09 1/11/10 1/12/09 ... 9/9/09
Why do I get 353 levels?

I am trying to identify the quarter hours with good weather in the arr
data frame. What I want to do is to go through the rows in gw, and to
set arr$gw to TRUE if arr$Date and arr$quarter match those in the gw row.

So I tried
gooddates = function(all, good) {
   la = length(all)   # All the flights
  lw = length(good)  # The good 15-minute periods
  for(j in 1:lw) {
    d=good$Date[j]
    q=good$quarter[j]
    all[all$DateTime==d && all$quarter==q,17]=TRUE
  }
}

but when I run this, I get
"Error in Ops.factor(all$DateTime, d) :
  level sets of factors are different"

I know the level sets are different, that is what I am trying to find.
But I think I am comparing single elements from the data frames.

So what am I doing wrong? And there ought to be a better way to do this.

Thanks in advance,
Jim Rome



More information about the R-help mailing list