2

I have a data frame :

$Date, $name, $value
1949-05-01, Hurricane, 5
1950-02-01, Hurricane, 6
1950-03-01,
1950-04-01,
1950-05-01,
1951-02-01,
1951-03-01,
1951-04-01,

These dates go all the way to 2015, with measurements for months 02, 03,04 and 05. I am trying to create a dataframe, or subset of this data that only has the rows that are for April (04). I have tried some code such as

aprilSWE <- hurricane.df[grep("^04", hurricane.df$Date),]

But I am not sure if I am using the ^ correctly. As the month "04" is in the middle of the date string, how can I use wildcards to select any row that has a "04" in the month as the date?

Thanks!

TomCho
  • 3,204
  • 6
  • 32
  • 83
Christopher
  • 189
  • 1
  • 10
  • No, you are not using `^` correctly. The `^` anchor asserts that the regular expression engine's current position in the string is the beginning of the string. – hwnd Apr 13 '15 at 17:01

2 Answers2

1

Try

hurricane.df[grep('^\\d{4}-04', hurricane.df$Date),]

Or convert to 'Date' class and use format

hurricane.df[format(as.Date(hurricane.df$Date), '%b'=='Apr',]
akrun
  • 874,273
  • 37
  • 540
  • 662
1

You shouldn't use string processing for this. Use the Date class:

DF <- read.table(text = "Date, name, value
    1949-05-01, Hurricane, 5
    1950-02-01, Hurricane, 6
    1950-03-01, Hurricane, 7
    1950-04-01, Hurricane, 8
    1950-05-01, Hurricane, 9
    1951-02-01, Hurricane, 10
    1951-03-01, Hurricane, 11
    1951-04-01, Hurricane, 12", header = TRUE, sep =",")

DF$Date <- as.Date(DF$Date)
DF[months(DF$Date) == "April",]
#        Date       name value
#4 1950-04-01  Hurricane     8
#8 1951-04-01  Hurricane    12
Roland
  • 127,288
  • 10
  • 191
  • 288