0
votes

Sorry about that I don't how to describe my question in Title.

My problem is that

I have a dataframe as below

| id | content | created_at          |
|----|---------|---------------------|
| 1  | hello   | 2014-12-10 00:00:00 |
| 2  | world   | 2013-11-11 00:00:00 |
| 3  | oh~no   | 2012-10-10 00:00:00 |
| 4  | helpme  | 2011-09-11 00:00:00 |

I want to subset this frame by time interval

for example:

  • subset: 2011 - 2012

    | 4 | helpme | 2011-09-11 00:00:00 |

  • subset: 2012 - 2013

    | 3 | oh~no | 2012-10-10 00:00:00 |

  • subset: 2013 - 2014

    | 2 | world | 2013-11-11 00:00:00 |

  • subset: 2014 - 2015

    | 1 | hello | 2014-12-10 00:00:00 |

Below is how I try to resolve this problem

  • I try to create a true,false array and do in each row

    ifelse( 
    
     difftime(DF$created_at,as.Date(ISOdate(seq(2004,2014),1,1))) >= 0 & 
    
     difftime(DF$created_at,as.Date(ISOdate(seq(2005,2015),1,1))) < 0
    
     , assign_to_subset_X, do_nothing)
    

but....

I don't think this is a good idea especially I already use R....

then I find some solutions such as apply

  apply(DF, 2, do_a_function_to_subset)

but I still have no idea to write this function

please give me a hint.

1

1 Answers

1
votes

Here is one possible solution

library(lubridate)
df <- read.table(textConnection("  id | content | created_at          
 1  | hello   | 2014-12-10 00:00:00 
 2  | world   | 2013-11-11 00:00:00 
 3  | oh~no   | 2012-10-10 00:00:00 
 4  | helpme  | 2011-09-11 00:00:00 "), header=TRUE, sep="|")


df$ts <- ymd_hms(df$created_at)

## create an interval
myInt <- ymd("2011-01-01") %--% ymd("2011-12-31")
df[df$ts %within% myInt, ]