-2

How can I categorise time values to month buckets in R?
Now

time                     
2014-01-31 20:46 +0000   
2014-01-31 10:46 +0000   
2014-02-21 20:16 +0000   
2014-02-11 12:36 +0000   

my goal

time                     month
2014-01-31 20:46 +0000   Jan
2014-01-31 10:46 +0000   Jan
2014-02-21 20:16 +0000   Feb
2014-02-11 12:36 +0000   Feb
co7000
  • 221
  • 1
  • 3
  • 12

2 Answers2

1
time <- c("2014-01-31 20:46 +0000",   
          "2014-01-31 10:46 +0000",   
          "2014-02-21 20:16 +0000",   
          "2014-02-11 12:36 +0000")

library(lubridate)
# first convert it to a proper date-time format and put it in a dataframe
df <- as.data.frame(as.POSIXct(time))
#then simply calculate month
df$month <- month(time, label= TRUE)

Which gives:

      as.POSIXct(time)  month
1 2014-01-31 20:46:00   Jan
2 2014-01-31 10:46:00   Jan
3 2014-02-21 20:16:00   Feb
4 2014-02-11 12:36:00   Feb
RHA
  • 3,677
  • 4
  • 25
  • 48
  • Your example works. I have an issue with my column. When I use your code example and type `typeof(time)` that's what I get back `[1] "character"`. When I try it with my data set I get this message `[1] "closure"`. Is it because I read a CSV file? – co7000 Oct 11 '15 at 21:27
  • @cosmin Probably. Look at (http://stackoverflow.com/questions/15057202/read-csv-and-colclasses?lq=1) – RHA Oct 12 '15 at 07:58
0

Here's my approach:

data <- read.csv("~/data.csv", 
                header=TRUE, 
                dec=".", 
                sep=",", 
                stringsAsFactors=FALSE)


time <- data$time
time <- as.Date(time, format="%Y-%m-%d %H:%M")
time


data$year <- as.factor(format(time, format ="%Y"))
data$month <- as.factor(format(time, format ="%m"))
data$day <- as.factor(format(time, format ="%d"))
co7000
  • 221
  • 1
  • 3
  • 12