Gago-Silva
Gago-Silva

Reputation: 1971

which() with objects of type character

I have a questions that might be too basic, but here it is...

I want to extract monthly data from a dataset like this:

    Date     Obs
1 2001-01-01 120
2 2001-01-02 100
3 2001-01-03 150
4 2001-01-04 175
5 2001-01-05 121
6 2001-01-06 100

I just want to get the rows from the data where I have a certain month(e.g. January), this works perfectly:

output=which(strftime(dataset[,1],"%m")=="01",dataset[,1])

However when I try to create a loop to go through all the months using a variable that is declared has character it doesn't work and I only get "FALSE".

value=as.character(k)
output=which(strftime(dataset[,1],"%m")==value,dataset[,1])

Upvotes: 1

Views: 192

Answers (3)

IRTFM
IRTFM

Reputation: 263451

You can also use dates as dates with POSIXlt()$mon

as.POSIXlt(output$date)$mon  # Note that Jan = 0 and Feb=1
 [1] 0 0 0 0 0 1 1 1 1 2

There are several other packages such as chron, lubridate and gdata that provide date handling functions. I found the functions in lubridate particularly intuitive and less prone to errors in my clumsy hands.

Upvotes: 2

Dirk is no longer here
Dirk is no longer here

Reputation: 368439

Do not parse dates as strings. That is too error prone. Parse dates as dates, and do logical comparisons on them.

Here is one approach, creating January to March data and sub-setting February based on a comparison:

R> output <- data.frame(date=seq(as.Date("2011-01-01"), by=7, length=10), 
+                       value=cumsum(runif(10)*100))
R> output
         date     value
1  2011-01-01   8.29916
2  2011-01-08  44.82950
3  2011-01-15  72.08662
4  2011-01-22 134.19277
5  2011-01-29 221.67744
6  2011-02-05 245.77195
7  2011-02-12 314.82081
8  2011-02-19 396.34661
9  2011-02-26 437.14286
10 2011-03-05 442.41321
R> output[ output[,"date"] >= as.Date("2011-02-01") & 
+          output[,"date"] <= as.Date("2011-02-28"), ]
        date   value
6 2011-02-05 245.772
7 2011-02-12 314.821
8 2011-02-19 396.347
9 2011-02-26 437.143
R> 

Another approach uses the xts package:

R> oo <- xts(output[,"value"], order.by=output[,"date"])
R> oo
                [,1]
2011-01-01   8.29916
2011-01-08  44.82950
2011-01-15  72.08662
2011-01-22 134.19277
2011-01-29 221.67744
2011-02-05 245.77195
2011-02-12 314.82081
2011-02-19 396.34661
2011-02-26 437.14286
2011-03-05 442.41321
R> oo["2011-02-01::2011-02-28"]
               [,1]                 
2011-02-05 245.772
2011-02-12 314.821
2011-02-19 396.347
2011-02-26 437.143
R> 

as xts has convenient date parsing for the index; see the package documentation for details.

Upvotes: 4

James
James

Reputation: 66844

I'm assuming k is an integer in 1:12. I suspect you may be better off using abbreviated month names:

value <- month.abb[k]
output <- which(strftime(dataset[,1],"%b")==value,dataset[,1])

The reason you way isn't working is because the month number is zero-padded and "1" != "01".

Upvotes: 3

Related Questions