-5

I'm pretty new in R. I have a data frame called 'dati' with 2 columns... the 1st column "Date" (are Date values), the 2nd one are "Values" (float values). 'dati' collects 7809 elements, and the "Date" values go from 1995 to 2015.

If I print 'dati' it is something like:

        "Date"      "Values"
 1      1995-01-01   7.987
 2      1995-01-02   7.944
 3      1995-01-03   7.901
    ...
 313    1995-12-29   5.187
    ...
 3033   2006-06-13   6.567
 3034   2006-06-14   6.588
 ...
 7809   2015-12-31   6.998

I would like to get a subset that contains only elements by filtering the Date values: I need to collect in my subset all the records where year is equals to '1995'... that is something like:

        "Date"      "Values"
 1      1995-01-01   7.987
 2      1995-01-02   7.944
 3      1995-01-03   7.901
    ...
 313    1995-12-29   5.187

Moreover, can you provide me a for loop where you increment the Date filtering value? For example, a for that makes 3 loops... at the 1st loop the filtering year Date value is '1995', at the 2nd one '1996' and at the 3rd one '1997'.

Please note that in both cases the subsets must contain both column values (Date an Numeric).

Thanks

baru
  • 401
  • 3
  • 9
  • 29
  • Please provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610) – Jaap Dec 09 '15 at 09:15
  • 1
    Why do you want to create all these subsets? There are better approaches to 'split data by something and do something to each part' than creating all subsets individually. – Heroka Dec 09 '15 at 10:07
  • I need to detect for each year the record with max number value, collect all the 11 records inside a list and then plot it. The same for the min values. – baru Dec 09 '15 at 10:28

1 Answers1

10

We can try subset

subset(dati, format(as.Date(Date),"%Y")==2005)

If we are trying to subset the data for each year, try split

split(dati, format(as.Date(dati$Date), "%Y"))
akrun
  • 874,273
  • 37
  • 540
  • 662