delaye
delaye

Reputation: 1399

R select row by year and month

I have à dataFrame like

head(JournalBeart)
        date     VTemperature
1 2012-03-08 10.2938450704225
2 2012-03-09 10.6240559440559
3 2012-03-10 12.4791398601399
4 2012-03-11 14.3567482517483
5 2012-03-12 15.9947622377622
6 2012-03-13 16.1366433566434

And I want to select line by month and year. I would select all rows that are for the months between April and June of 2012 I have find for month,

month(JournalBeart$date) %in% 3:9

how to integrate the month and year

Thanks!

Upvotes: 0

Views: 4773

Answers (2)

Froom2
Froom2

Reputation: 1279

Unless your date column is already in date format,

JournalBeart$date <- as.Date(as.character(JournalBeart$date), "%Y-%m-%d")

Then you can use a logical argument to select the date range you want:

JournalBeart[JournalBeart$date >= as.Date("010412", "%d%m%y") & JournalBeart$date <= as.Date("300612", "%d%m%y"), ]

Upvotes: 1

bartektartanus
bartektartanus

Reputation: 16080

require(lubridate) 
month(JournalBeart$date) %in% 3:9 & year(JournalBeart$date)==2012

or

as.numeric(format(date, "%m")) #month
as.numeric(format(date, "%Y")) #year

Upvotes: 2

Related Questions