Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

datetime - How to filter or subset specific date and time intervals in R? Lubridate?

I have a dataset consisting of two columns. A datetime column and a column with numerical values. Its a simple dataset, so I did not attach it..

What I need to do, is to filter or subset the data corresponding with a class schedule, so that I get a dataset/dataframe with datetime values and numerical values for the time when the class has lectures only.

The class schedule is different from each day of the week, e.g. Mondays 8:00-9.50, 10.30-11.30, 14.50-15:50. Tuesdays 10.30-11.30, 14.10-15.30, Wednesdays...an so on.

Any idea how I could do this?

I usually convert datetime-values to POSIXct format, but recently I read about lubridate.

I am just still not sure how to efficiently subset with all these criteria.

Perhaps I should subset the data according to the weekdays first. And then subset the different weekdays according to the lecture time...

Hope someone can help me.

BTW: The data is for all of 2014, so I actually have to avoid the data when the class have holidays as well...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Convert class intervals to an interval class in lubridate. Then subset based on the test of if the dates are in the intervals...

> a <- new_interval(Sys.time(), Sys.time() + 120)
> Sys.time() %within% a
[1] TRUE

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...