0

As part of an assignment in college I am trying to make a small r package that provides some basic statistics and graphics on GTFS feeds.

I am using the files from https://github.com/ondrejivanic/131500/blob/master/gtfs.r.

I have to create a number of S4 classes to as part of the assignment. I have created a separate classes for each GTFS feed file. I am trying to make a list of service id's to produce a graphic for the number of trips on a given day.

Here I define and create and object of the class.

# Create the S4 Class for calendar_dates.txt
# calendar_dates.txt - service_id, date, exception_type

 setClass("CalendarDates", representation(service_id = "factor", date = "POSIXct",     exception_type = "numeric"))

# create new object of SHAPES from files
calendar_dates <- transform(
  read.gtfs.file("calendar_dates.txt", "data"),
  date =  ymd(date)
)
# create S4 object of routes
calendar_datesS4 <- new("CalendarDates", service_id = calendar_dates$service_id, date =     calendar_dates$date, exception_type = calendar_dates$exception_type)

The part I cannot understand is how to perform this subset on an S4 object. The piece below works with a dataframe object:

 calendar.dates <- calendar_datesS4 
 calendar.dates[calendar.dates$date == d & calendar.dates$exception_type == 1, c("service_id")]
  [1] "daily_1" "daily_2" "daily_3" "daily_4"

Doing the following results in an error:

calendar.dates[calendar.dates@date == d & calendar.dates@exception_type == 1, c("service_id")]
Error in calendar.dates[dates == d & exceptions == 2, c("service_id")] : 

object of type 'S4' is not subsettable

I have not found any questions elsewhere, where a condition must be met for the subset.

I really appreciate any help with this!

  • That's because by default S4 classes will not have a `[` method defined. You must define one. I think [this answer](http://stackoverflow.com/questions/10961842/how-to-define-the-subset-operators-for-a-s4-class) should help. You can make `[` work however you want it to. – MrFlick Aug 02 '14 at 21:24
  • Ok I have followed the steps in the answer you posted, thanks! But how would I make this a more generic method? For instance I am only returning the **x@service_id[i]** slot. How would I feed the names of the slots to the return? – Rusty Broderick Aug 02 '14 at 22:32

0 Answers0