[R] Subset a data frame with specific date

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Tue Jan 14 07:20:52 CET 2020


The dput function is for re-creating an R object in another R workspace, so it uses fundamental base types to define objects. A Date is really the number of days since a specific date (typically 1970-01-01) that get converted to look like dates whenever you display or print them, so what you are seiing are those numbers. If we enter the R code returned by dput into our R session we will be able to see the dates.

Your mjo30 table seems to call the day of the month the "date"... which is confusing. I would combine those three columns into one like

mjo30$Dt <- as.Date( ISOdate( mjo30$year, mjo30$month, mjo30$date ) )

You could then use indexing

mjo30[ date[1] == mjo30$Dt, ]

or

mjo30[ mjo30$Dt %in% date, ]

but the subset function would not work in this case because you have two different objects (a column in mjo30 and a vector in your global environment) both referred to as 'date'.

On January 13, 2020 8:53:38 PM PST, ani jaya <gaaauul using gmail.com> wrote:
>Good morning R-Help,
>
>I have a dataframe with 7 columns and 10000+ rows. I want to
>subset/extract
>those data frame with specific date (not in order). Here the head of my
>data frame:
>
>head(mjo30)  year month date      rmm1     rmm2 phase     amp
>1 1986     1    1 -0.326480 -1.55895     2 1.59277
>2 1986     1    2 -0.417700 -1.82689     2 1.87403
>3 1986     1    3  0.032915 -2.40150     3 2.40172
>4 1986     1    4  0.492743 -2.49216     3 2.54041
>5 1986     1    5  0.585106 -2.76866     3 2.82981
>6 1986     1    6  0.665013 -3.13883     3 3.20851
>
>and here my specific date:
>> date [1] "1986-04-25" "1987-06-10" "1988-09-03" "1989-10-05"
>"1990-10-26" "1991-05-07" "1992-11-19" "1993-01-23" "1994-12-04"
>[10] "1995-05-11" "1996-10-04" "1997-04-29" "1998-04-08" "1999-01-16"
>"2000-08-01" "2001-10-02" "2002-05-08" "2003-04-01"
>[19] "2004-05-07" "2005-09-02" "2006-12-30" "2007-09-03" "2008-10-24"
>"2009-11-14" "2010-07-05" "2011-04-30" "2012-05-21"
>[28] "2013-04-07" "2014-05-07" "2015-07-26"
>
>And also I was confused when I dput my date, it show like this:
>> dput(date)structure(c(5958, 6369, 6820, 7217, 7603, 7796, 8358, 8423,
>9103,
>9261, 9773, 9980, 10324, 10607, 11170, 11597, 11815, 12143, 12545,
>13028, 13512, 13759, 14176, 14562, 14795, 15094, 15481, 15802,
>16197, 16642), class = "Date")
>
>what is that mean? I mean why it is not recall the dates but some
>values (5958,6369,7217,..)?
>
>Any comment and recommendation is appreciate.  Thank you.
>
>Best,
>
>Ani
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list