What strategy suggest to subset a dataframe by month and year to obtain a single list of data frames in r

by spectral_lion_18   Last Updated September 13, 2018 23:26 PM

I want to subset by month and year (ie. 01-2000) a DF to obtain a single list of data frames to continue work with them, dates are not continuous and some months have two rows one row, i tried this:

# DF is my Original data frame
# Defining the vectors of month and yaers to subset
     months <- c("01","02","03","04","05","06","07","08","09","10","11","12")
     years <- as.character(seq(2000,2017,by=1))

# Obtaining a list of DF subset
     list_blank <- list()
     for (j in 1:length(years)){
     list_dirs <- list()
            for(k in 1:length(months)){
            list_dirs[[k]] <- subset(DF, format.Date(DATE,"%m")==months[k] & 
                                          format.Date(DATE, "%Y")==years[j])
            }
     list_blank[[j]] <- list_dirs[sapply(list_dirs, nrow)>0]
     }
list_blank

But in list _blank... i just obtain a partial list of DFs.

What strategy suggest me to subset my data in order to obtain a single list of data frames?

Any help is welcome.

My original DF is:

DF<-as.data.frame(structure(list(structure(1:10,.Label=c("2000-06-29","2000-07-15","2000-09-17", "2000-10-03", "2000-10-19", "2000-11-04", "2000-11-20", 
"2000-12-06", "2001-05-31", "2001-06-16", "2001-07-03", "2001-08-04", 
"2001-09-21", "2001-10-23", "2002-01-27", "2002-03-16", "2002-05-03", 
"2002-07-23", "2002-09-09", "2002-10-11", "2002-11-12", "2002-12-30", 
"2003-03-20", "2003-04-05", "2003-04-21", "2003-05-07", "2003-07-27", 
"2003-08-12", "2003-08-28", "2003-09-29", "2003-10-31", "2004-04-24", 
"2004-05-10", "2004-05-26", "2004-06-28", "2004-07-14", "2004-08-31", 
"2004-09-16", "2004-11-03", "2005-02-23", "2005-04-04", "2005-04-12", 
"2005-05-14", "2005-05-22", "2005-06-07", "2005-07-02", "2005-07-18", 
"2005-07-26", "2005-08-19", "2005-09-12", "2005-10-06", "2005-10-22", 
"2005-11-07", "2005-11-15", "2006-02-27", "2006-04-24", "2006-05-02", 
"2006-06-11", "2006-07-14", "2006-09-08", "2006-09-16", "2006-10-18", 
"2007-02-15", "2007-03-19", "2007-04-04", "2007-05-06", "2007-07-02", 
"2007-07-26", "2007-08-03", "2007-10-30", "2007-11-15", "2007-12-01", 
"2007-12-17", "2008-02-11", "2008-05-01", "2008-05-09", "2008-06-02", 
"2008-06-10", "2008-06-27", "2008-07-13", "2008-08-22", "2008-09-07", 
"2008-10-01", "2008-10-17", "2008-11-02", "2008-11-10", "2008-11-18", 
"2009-01-05", "2009-01-21", "2009-02-22", "2009-03-26", "2009-04-11", 
"2009-04-27", "2009-05-13", "2009-07-01", "2009-09-19", "2009-11-06", 
"2009-11-22", "2009-12-08", "2010-02-10", "2010-03-30", "2010-05-01", 
"2010-06-18", "2010-07-05", "2010-08-22", "2010-09-07", "2010-09-23", 
"2010-10-25", "2010-11-10", "2011-03-18", "2011-04-19", "2011-05-05", 
"2011-11-14", "2011-12-16", "2012-01-01", "2012-02-18", "2012-05-08", 
"2012-06-09", "2012-08-13", "2012-08-29", "2012-09-30", "2012-11-01", 
"2012-11-17", "2013-08-01", "2013-08-17", "2013-09-02", "2013-09-18", 
"2013-10-04", "2013-11-05", "2013-12-23", "2014-04-14", "2014-04-30", 
"2014-05-16", "2014-06-01", "2014-08-05", "2014-11-25", "2015-06-05", 
"2015-07-24", "2015-08-09", "2015-09-26", "2015-11-13", "2015-11-29", 
"2015-12-15", "2016-05-07", "2016-05-23", "2016-06-08", "2016-06-24", 
"2016-07-11", "2016-07-27", "2016-08-28", "2016-09-13", "2017-05-11"
), class = "factor"), c(9.019002745, 13.38280903, 14.2359526, 
7.02268875, 12.0105926, 6.582598703, 43.91183269, 41.9100792, 
-0.963859179, 41.77771314), c(4.047206988, -0.978071864, 8.064343633, 
-9.940021903, 5.031642336, -2.854467401, 3.003841743, -3.977130333, 
-0.001780529, -3.996571923), structure(c(1L, 65L, 76L, 87L, 98L, 
109L, 120L, 131L, 142L, 2L), .Label = c("/R/user/data1.txt", 
"/R/user/data10.txt", "/R/user/data100.txt", "/R/user/data101.txt", 
"/R/user/data102.txt", "/R/user/data103.txt", "/R/user/data104.txt", 
"/R/user/data105.txt", "/R/user/data106.txt", "/R/user/data107.txt", 
"/R/user/data108.txt", "/R/user/data109.txt", "/R/user/data11.txt", 
"/R/user/data110.txt", "/R/user/data111.txt", "/R/user/data112.txt", 
"/R/user/data113.txt", "/R/user/data114.txt", "/R/user/data115.txt", 
"/R/user/data116.txt", "/R/user/data117.txt", "/R/user/data118.txt", 
"/R/user/data119.txt", "/R/user/data12.txt", "/R/user/data120.txt", 
"/R/user/data121.txt", "/R/user/data122.txt", "/R/user/data123.txt", 
"/R/user/data124.txt", "/R/user/data125.txt", "/R/user/data126.txt", 
"/R/user/data127.txt", "/R/user/data128.txt", "/R/user/data129.txt", 
"/R/user/data13.txt", "/R/user/data130.txt", "/R/user/data131.txt", 
"/R/user/data132.txt", "/R/user/data133.txt", "/R/user/data134.txt", 
"/R/user/data135.txt", "/R/user/data136.txt", "/R/user/data137.txt", 
"/R/user/data138.txt", "/R/user/data139.txt", "/R/user/data14.txt", 
"/R/user/data140.txt", "/R/user/data141.txt", "/R/user/data142.txt", 
"/R/user/data143.txt", "/R/user/data144.txt", "/R/user/data145.txt", 
"/R/user/data146.txt", "/R/user/data147.txt", "/R/user/data148.txt", 
"/R/user/data149.txt", "/R/user/data15.txt", "/R/user/data150.txt", 
"/R/user/data151.txt", "/R/user/data152.txt", "/R/user/data16.txt", 
"/R/user/data17.txt", "/R/user/data18.txt", "/R/user/data19.txt", 
"/R/user/data2.txt", "/R/user/data20.txt", "/R/user/data21.txt", 
"/R/user/data22.txt", "/R/user/data23.txt", "/R/user/data24.txt", 
"/R/user/data25.txt", "/R/user/data26.txt", "/R/user/data27.txt", 
"/R/user/data28.txt", "/R/user/data29.txt", "/R/user/data3.txt", 
"/R/user/data30.txt", "/R/user/data31.txt", "/R/user/data32.txt", 
"/R/user/data33.txt", "/R/user/data34.txt", "/R/user/data35.txt", 
"/R/user/data36.txt", "/R/user/data37.txt", "/R/user/data38.txt", 
"/R/user/data39.txt", "/R/user/data4.txt", "/R/user/data40.txt", 
"/R/user/data41.txt", "/R/user/data42.txt", "/R/user/data43.txt", 
"/R/user/data44.txt", "/R/user/data45.txt", "/R/user/data46.txt", 
"/R/user/data47.txt", "/R/user/data48.txt", "/R/user/data49.txt", 
"/R/user/data5.txt", "/R/user/data50.txt", "/R/user/data51.txt", 
"/R/user/data52.txt", "/R/user/data53.txt", "/R/user/data54.txt", 
"/R/user/data55.txt", "/R/user/data56.txt", "/R/user/data57.txt", 
"/R/user/data58.txt", "/R/user/data59.txt", "/R/user/data6.txt", 
"/R/user/data60.txt", "/R/user/data61.txt", "/R/user/data62.txt", 
"/R/user/data63.txt", "/R/user/data64.txt", "/R/user/data65.txt", 
"/R/user/data66.txt", "/R/user/data67.txt", "/R/user/data68.txt", 
"/R/user/data69.txt", "/R/user/data7.txt", "/R/user/data70.txt", 
"/R/user/data71.txt", "/R/user/data72.txt", "/R/user/data73.txt", 
"/R/user/data74.txt", "/R/user/data75.txt", "/R/user/data76.txt", 
"/R/user/data77.txt", "/R/user/data78.txt", "/R/user/data79.txt", 
"/R/user/data8.txt", "/R/user/data80.txt", "/R/user/data81.txt", 
"/R/user/data82.txt", "/R/user/data83.txt", "/R/user/data84.txt", 
"/R/user/data85.txt", "/R/user/data86.txt", "/R/user/data87.txt", 
"/R/user/data88.txt", "/R/user/data89.txt", "/R/user/data9.txt", 
"/R/user/data90.txt", "/R/user/data91.txt", "/R/user/data92.txt", 
"/R/user/data93.txt", "/R/user/data94.txt", "/R/user/data95.txt", 
"/R/user/data96.txt", "/R/user/data97.txt", "/R/user/data98.txt", 
"/R/user/data99.txt"), class = "factor")), .Names = c("DATE", 
"TEMP", "BIO", "DIR")))


Answers 1


I'm not entirely sure I understand what you want to do. But perhaps this should get you started. I'd use lubridate

library(lubridate)
library(tidyverse)
DF %>%
    mutate(DATE = ymd(DATE)) %>%
    filter(month(DATE) %in% as.numeric(months) & year(DATE) %in% as.numeric(years))
#    DATE       TEMP          BIO                DIR
#1  2000-06-29  9.0190027  4.047206988  /R/user/data1.txt
#2  2000-07-15 13.3828090 -0.978071864  /R/user/data2.txt
#3  2000-09-17 14.2359526  8.064343633  /R/user/data3.txt
#4  2000-10-03  7.0226888 -9.940021903  /R/user/data4.txt
#5  2000-10-19 12.0105926  5.031642336  /R/user/data5.txt
#6  2000-11-04  6.5825987 -2.854467401  /R/user/data6.txt
#7  2000-11-20 43.9118327  3.003841743  /R/user/data7.txt
#8  2000-12-06 41.9100792 -3.977130333  /R/user/data8.txt
#9  2001-05-31 -0.9638592 -0.001780529  /R/user/data9.txt
#10 2001-06-16 41.7777131 -3.996571923 /R/user/data10.txt
Maurits Evers
Maurits Evers
September 13, 2018 23:24 PM

Related Questions


R Subsetting a dataframe by date-range

Updated August 12, 2017 01:26 AM


Data Frame containing hyphens using R

Updated February 15, 2018 18:26 PM