Vinay billa Vinay billa - 2 months ago 4x
R Question

Subset dataframes stored in a list

I have a list containing 16 dataframes all of which have only two columns

DateTime Value
2009-05-31 100
2009-06-30 200
2009-07-31 300
and so on

I want to subset all the dataframes in the list based on a date filter. I tried the following code but it did not work out

> c <- function(df) {
+ within(df, DateTime >= "2009-06-30")
+ }
> train_data <- lapply(my_data, c)

Am I missing out on something here ?

Thank You.


Try this (assuming the Datetime columns in your data frames are formatted as Dates):

c <- function(df){
df[df$DateTime >= as.Date("2009-06-30"),]

train_data <- lapply(my_data, c)