Data frame of departure and return dates, how do I get a list of all dates away?

StackOverflow https://stackoverflow.com/questions/21784050

  •  11-10-2022
  •  | 
  •  

Вопрос

I'm stuck on a problem calculating travel dates. I have a data frame of departure dates and return dates.

Departure  Return
1    7/6/13  8/3/13
2    7/6/13  8/3/13
3   6/28/13  8/7/13

I want to create and pass a function that will take these dates and form a list of all the days away. I can do this individually by turning each column into dates.

## Turn the departure and return dates into a readable format
Dept <- as.Date(travelDates$Dept, format = "%m/%d/%y")
Retn <- as.Date(travelDates$Retn, format = "%m/%d/%y")
travel_dates <- na.omit(data.frame(dept_dates,retn_dates))

seq(from = travel_dates[1,1], to = travel_dates[1,2], by = 1)

This gives me [1] "2013-07-06" "2013-07-07"... and so on. I want to scale to cover the whole data frame, but my attempts have failed.

Here's one that I thought might work.

days_abroad <- data.frame()
get_days <- function(x,y){
  all_days <- seq(from = x, to = y, by =1)
  c(days_abroad, all_days)
  return(days_abroad)
}
get_days(travel_dates$dept_dates, travel_dates$retn_dates)

I get this error:

Error in seq.Date(from = x, to = y, by = 1) : 'from' must be of length 1 

There's probably a lot wrong with this, but what I would really like help on is how to run multiple dates through seq().

Sorry, if this is simple (I'm still learning to think in r) and sorry too for any breaches in etiquette. Thank you.

Это было полезно?

Решение

EDIT: updated as per OP comment.

How about this:

travel_dates[] <- lapply(travel_dates, as.Date, format="%m/%d/%y")
dts <- with(travel_dates, mapply(seq, Departure, Return, by="1 day"))

This produces a list with as many items as you had rows in your initial table. You can then summarize (this will be data.frame with the number of times a date showed up):

data.frame(count=sort(table(Reduce(append, dts)), decreasing=T))

#            count
# 2013-07-06     3
# 2013-07-07     3
# 2013-07-08     3
# 2013-07-09     3
# ...

OLD CODE:

The following gets the #days of each trip, rather than a list with the dates.

transform(travel_dates, days_away=Return - Departure + 1)

Which produces:

#    Departure     Return days_away
# 1 2013-07-06 2013-08-03   29 days
# 2 2013-07-06 2013-08-03   29 days
# 3 2013-06-28 2013-08-07   41 days

If you want to put days_away in a separate list, that is trivial, though it seems more useful to have it as an additional column to your data frame.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top