0

I couldn't find a solution of my problem with POSIXct format - I have a monthly data. This is a scrap of my code:

Data <- as.POSIXct(as.character(czerwiec$Data), format = "%Y-%m-%d %H:%M:%S")
get.rows <- Data >= as.POSIXct(as.character("2013-06-03 00:00:01")) & Data <= as.POSIXct(as.character("2013-06-09 23:59:59")) 
czerwiec <- czerwiec[get.rows,]
Data <- Data[get.rows]

I chose one hole week of June from 3 to 9 and wanted to estimate the sum of column X (czerwiec$X) by every hours. As you see I could reduce time, but it will be stupid to do it, like this

get.rows <- Data >= as.POSIXct(as.character("2013-06-03 00:00:01")) & 
  Data <= as.POSIXct(as.character("2013-06-03 00:59:59")) 

then

get.rows <- Data >= as.POSIXct(as.character("2013-06-04 00:00:01")) & 
  Data <= as.POSIXct(as.character("2013-06-04 00:59:59"))

And in the end of this operations, I can estimate sum for this hour etc.

Do you have any idea, how I can recall to every rows, which have time like 2013-06-03 to 2013-06-09 and 00:00:01 to 00:59:59??

Something about data frame "czerwiec", so I have three columns, where first call "ID", second "Price" and third "Data" (means Date).

Thx for help :)

user2314737
  • 27,088
  • 20
  • 102
  • 114
Blazej
  • 19
  • 3
  • 1
    Please add code to allow us to reproduce the data frame `czerwiec`. – Drew Steen Aug 30 '13 at 15:03
  • Drew Steen, do you have enough information to help me ??? Thx in advance :) – Blazej Aug 31 '13 at 08:25
  • Please see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for how to make a reproducible example - this will help SO users understand better what your data are and what you are trying to do with them – Drew Steen Aug 31 '13 at 12:51

1 Answers1

0

This might help. I've used the lubridate package, which doesn't really do anything you can't do in base R, but it makes handling dates much easier

# Set up Data as a string vector
Data <- c("2013-06-01 05:05:05", "2013-06-06 05:05:05", "2013-06-06 08:10:05", "2013-07-07 05:05:05")

require(lubridate)

# Set up the data frame with fake data. This makes a reproducible example
set.seed(4) #For reproducibility, always set the seed when using random numbers

# Create a data frame with Data and price
czerwiec <- data.frame(price=runif(4))

# Use lubridate to turn the Data string into a vector of POSIXctn objects
czerwiec$Data <- ymd_hms(Data) 

# Determine the 'yearday' -i.e. yearday of Jan 1 is 1; yearday of Dec 31 is 365 (or 366 in a leap year)
czerwiec$yday <- yday(czerwiec$Data) 

# in.range is true if the date is in the desired date range
czerwiec$in.range <- czerwiec$yday[czerwiec$yday >= yday(ymd("2013-06-03")) & 
                                     czerwiec$yday yday(ymd("2013-06-09")] 

# Pick out the dates that have the range that you want
selected_dates <- subset(czerwiec, in.range==TRUE)
Drew Steen
  • 16,045
  • 12
  • 62
  • 90