Subset by date in R

I am trying to subset from my dataframe, except for those before 01/01/2012. The date format is in %d-%-m-%y

, and the program already knows that it is a date. Data frame TA

, variable DATE_OLD

. I've tried:, new <- subset(TA, TA$JDATE_OLD<"01-01-2012")

with obvious success.

It gives me back what $

is an invalid operator for atomic vectors. Any help would be appreciated. Thank.

+3


source to share


1 answer


You need to have a correct comparison.

Let's start with an arbitrary data frame:

R> df <- data.frame(date=seq(as.Date("2015-01-01"), \
+                         as.Date("2015-12-01"), by="month"), \
+                   value=1000+(0:11)*42)
R> df
         date value
1  2015-01-01  1000
2  2015-02-01  1042
3  2015-03-01  1084
4  2015-04-01  1126
5  2015-05-01  1168
6  2015-06-01  1210
7  2015-07-01  1252
8  2015-08-01  1294
9  2015-09-01  1336
10 2015-10-01  1378
11 2015-11-01  1420
12 2015-12-01  1462
R> 

      



Now, if you want all dates in August, you just index for a higher cutoff value - and make sure it's a type as well Date

:

R> df[ df$date > as.Date("2015-08-01"), ]
         date value
9  2015-09-01  1336
10 2015-10-01  1378
11 2015-11-01  1420
12 2015-12-01  1462
R> 

      

+4


source







All Articles