Counting POSIXlt times by day - r

I have a chunk of POSIXlt times in a data frame, and I'm trying to see how many occurrences of these observances (in this case, bike rides) I have per day. What's the best way to do that?
The dates look like this:
> rides$start.fmtd[1:25]
[1] "2014-01-01 00:06:00" "2014-01-01 00:11:00" "2014-01-01 00:12:00"
[4] "2014-01-01 00:14:00" "2014-01-01 00:15:00" "2014-01-01 00:16:00"
[7] "2014-01-01 00:16:00" "2014-01-01 00:19:00" "2014-01-01 00:20:00"
[10] "2014-01-01 00:20:00"
dput(head()) gives me this:
> dput(head(rides$start.fmtd))
structure(list(sec = c(0, 0, 0, 0, 0, 0), min = c(6L, 11L, 12L,
14L, 15L, 16L), hour = c(0L, 0L, 0L, 0L, 0L, 0L), mday = c(1L,
1L, 1L, 1L, 1L, 1L), mon = c(0L, 0L, 0L, 0L, 0L, 0L), year = c(114L,
114L, 114L, 114L, 114L, 114L), wday = c(3L, 3L, 3L, 3L, 3L, 3L
), yday = c(0L, 0L, 0L, 0L, 0L, 0L), isdst = c(0L, 0L, 0L, 0L,
0L, 0L)), .Names = c("sec", "min", "hour", "mday", "mon", "year",
"wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"))
This specific frame has about 300,000 observances (It's the capitol bikeshare dataset, which contains every bike ride taken in the system, packaged quarterly).

dates <- as.POSIXlt(runif(10, 0, 60 * 60 * 24 * 7), origin = Sys.Date())
dates
## [1] "2014-06-16 03:36:13 PDT" "2014-06-15 22:39:41 PDT"
## [3] "2014-06-19 12:25:11 PDT" "2014-06-17 09:31:45 PDT"
## [5] "2014-06-20 02:20:00 PDT" "2014-06-18 04:36:48 PDT"
## [7] "2014-06-19 17:33:35 PDT" "2014-06-21 15:38:24 PDT"
## [9] "2014-06-17 08:50:45 PDT" "2014-06-20 03:36:38 PDT"
class(dates)
## [1] "POSIXlt" "POSIXt"
table(as.Date(dates))
## 2014-06-15 2014-06-16 2014-06-17 2014-06-18 2014-06-19 2014-06-20 2014-06-21
## 1 1 2 1 2 2 1

POSIXlt has a yday attribute, and you can use this to do a count, using aggregate or by or table or such.
For example, suppose you have a count of observances of day in count in a data frame d, with column date. If your data does not span more than one year, you can use yday alone:
aggregate(count ~ date$yday, data=d, FUN=sum)
If it spans more than one year (or just to be safe) you can also include the year (with any multiplier greater than 366):
aggregate(count ~ I(1000*date$year + date$yday), data=d, FUN=sum)

If you have values with dates and times, you can format them to just have the date and use table() on those values to get counts.
#sample data
set.seed(15)
randomdates <- structure(runif(30, 1357016400, 1359608400),
class=c("POSIXct", "POSIXt"), tzone="")
Now count values per date
table(strftime(randomdates, "%Y-%m-%d"))
The only downside to this is that table() turns the dates to character vectors. You can convert them back with
tbl<-table(strftime(randomdates, "%Y-%m-%d"))
as.POSIXct(names(tbl))

Related

How to create a Time Column Using the time from a Date/Time column in R?

I had a data frame with a column labelled Date_Time_GMT_3 which contained date/times. I used the Date_Time_GMT_3 column to create another data frame with 3 extra columns that have the month year and day seperated. This new data frame looks like so:
df = structure(list(Date_Time_GMT_3 = structure(list(sec = c(0, 0,
0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L), hour = c(8L, 8L,
8L, 8L, 8L, 8L), mday = c(1L, 1L, 1L, 1L, 1L, 1L), mon = c(5L,
5L, 5L, 5L, 5L, 5L), year = c(121L, 121L, 121L, 121L, 121L, 121L
), wday = c(2L, 2L, 2L, 2L, 2L, 2L), yday = c(151L, 151L, 151L,
151L, 151L, 151L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L), zone = c("EST",
"EST", "EST", "EST", "EST", "EST"), gmtoff = c(NA_integer_, NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), tzone = "EST", class = c("POSIXlt",
"POSIXt")), name = c("X20676880_X3WR_AIR_Stationary", "X20819740_X3WR_U_Stationary",
"X20819740_X3WR_S_Stationary", "X21092860_X3WR_U_Compare", "X20676883_13WR_U_Stationary",
"X20676883_13WR_S_Stationary"), value = c(11.431, 11.625, NA,
NA, 10.651, NA), month = c(6, 6, 6, 6, 6, 6), year = c(2021,
2021, 2021, 2021, 2021, 2021), day = c(1L, 1L, 1L, 1L, 1L, 1L
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))
The code I used to get the month day and year columns from the Date_Time_GMT_3 column looks like this
mutate(month = lubridate::month(Date_Time_GMT_3),
year = lubridate::year(Date_Time_GMT_3),
day = lubridate::day(Date_Time_GMT_3))
Is there a way to use the lubridate function to get a time column. I've tried this line of code
mutate(month = lubridate::month(Date_Time_GMT_3),
year = lubridate::year(Date_Time_GMT_3),
day = lubridate::day(Date_Time_GMT_3),
#New LINE OF CODE
time = lubridate::hms(Date_Time_GMT_3))
When I use that new line of code I get this error
Warning message:
Problem with `mutate()` column `TIME`.
i `TIME = lubridate::hms(Date_Time_GMT_3)`.
i Some strings failed to parse, or all strings are NAs
Any ideas how to make it work?
It doesn't work because hms() expects only numbers in triples, where you have a date before the time, so you need to remove that portion before passing it to hms(). I have used substr since all the dates must have the same format, in this case, YYYY-MM-DD, so keep everything starting from the 11th character.
lubridate::hms(substr(df$Date_Time_GMT_3, 11, nchar(df$Date_Time_GMT_3)))
[1] "8H 0M 0S" "8H 0M 0S" "8H 0M 0S" "8H 0M 0S" "8H 0M 0S" "8H 0M 0S"
In dplyr
df %>%
mutate(hms = lubridate::hms(substr(Date_Time_GMT_3, 11, nchar(Date_Time_GMT_3))))
# A tibble: 6 x 7
Date_Time_GMT_3 name value month year day hms
<dttm> <chr> <dbl> <dbl> <dbl> <int> <Period>
1 2021-06-01 08:00:00 X20676880_X3WR_AIR_Station~ 11.4 6 2021 1 8H 0M 0S
2 2021-06-01 08:00:00 X20819740_X3WR_U_Stationary 11.6 6 2021 1 8H 0M 0S
3 2021-06-01 08:00:00 X20819740_X3WR_S_Stationary NA 6 2021 1 8H 0M 0S
4 2021-06-01 08:00:00 X21092860_X3WR_U_Compare NA 6 2021 1 8H 0M 0S
5 2021-06-01 08:00:00 X20676883_13WR_U_Stationary 10.7 6 2021 1 8H 0M 0S
6 2021-06-01 08:00:00 X20676883_13WR_S_Stationary NA 6 2021 1 8H 0M 0S

Set the start point for time intervals in R

I have different sets of data with the following format
Time Value1 Value2 ....
11/04/2015 15:12:22 1 2 ....
11/04/2015 15:13:46 1 2 ....
And I want to group them in intervals of 15 minutes. I can do this with the following code
data$time = cut(data$time, breaks = "15 min")
data.grouped <- aggregate(data[,c(-1)], by = list(time = datos$time), median)
The problem is that the time field in the output has the following values
12/04/2015 16:12
12/04/2015 16:27
12/04/2015 16:42
12/04/2015 16:57
And I want the times to be :00 :15 :30 or :45. Is there any way of forcing the intervals to be like this or a different approach to merge the data that allows it?
A sample data from dput:
structure(list(time = structure(list(sec = c(49, 5, 21, 37, 54,
10, 38), min = c(12L, 13L, 13L, 13L, 13L, 14L, 22L), hour = c(15L,
15L, 15L, 15L, 15L, 15L, 16L), mday = c(11L, 11L, 11L, 11L, 11L,
11L, 12L), mon = c(3L, 3L, 3L, 3L, 3L, 3L, 3L), year = c(116L,
116L, 116L, 116L, 116L, 116L, 116L), wday = c(1L, 1L, 1L, 1L,
1L, 1L, 2L), yday = c(101L, 101L, 101L, 101L, 101L, 101L, 102L
), isdst = c(1L, 1L, 1L, 1L, 1L, 1L, 1L), zone = c("CEST", "CEST",
"CEST", "CEST", "CEST", "CEST", "CEST"), gmtoff = c(NA_integer_,
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,
NA_integer_)), .Names = c("sec", "min", "hour", "mday", "mon",
"year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt",
"POSIXt")), value1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("time",
"value1"), row.names = c(NA, -7L), class = "data.frame")
Starting with your dput, calling it df, first we'll convert your factor to a POSIXct class, then we will floor it to closest 15 minutes below. (use round instead of floor if you want the closest 15 minutes in general):
df$time = as.POSIXct(df$time)
df$time15 = lubridate::floor_date(df$time, unit = "15 min")
df
# time value1 time15
# 1 2016-04-11 15:12:49 0 2016-04-11 15:00:00
# 2 2016-04-11 15:13:05 0 2016-04-11 15:00:00
# 3 2016-04-11 15:13:21 0 2016-04-11 15:00:00
# 4 2016-04-11 15:13:37 0 2016-04-11 15:00:00
# 5 2016-04-11 15:13:54 0 2016-04-11 15:00:00
# 6 2016-04-11 15:14:10 0 2016-04-11 15:00:00
# 7 2016-04-12 16:22:38 0 2016-04-12 16:15:00
You can then aggregate using the time15 column as the grouper.
I provide an example you can replicate with your data frame. First, I create a dummy time series (ts) as.POSIXct by 5 min intervals and then group them by 15 min intervals using dplyr.
ts <- seq.POSIXt(as.POSIXct("2017-01-01", tz = "UTC"),
as.POSIXct("2017-02-01", tz = "UTC"),
by = "5 min")
ts <- as.data.frame(ts)
library(dplyr)
ts %>%
group_by(interval = cut(ts, breaks = "15 min")) %>%
summarise(count= n())
Output
# A tibble: 2,977 x 2
interval sumvalue
<fct> <int>
1 2017-01-01 00:00:00 3
2 2017-01-01 00:15:00 3
3 2017-01-01 00:30:00 3
4 2017-01-01 00:45:00 3
5 2017-01-01 01:00:00 3
6 2017-01-01 01:15:00 3
7 2017-01-01 01:30:00 3
8 2017-01-01 01:45:00 3
9 2017-01-01 02:00:00 3
10 2017-01-01 02:15:00 3
# ... with 2,967 more rows

Record variable value when condition true with dynamic name

I have 9x2 dataframe DATS with prices and POSIXct datetimestamps sampled every 15 minutes. and a list of dates FOMCDATES with the dates of recent FOMC events. I then split the POSIXct datetimestamps into separate Date and Time columns. I then add column FOMCBinary to DATS containing a 1 whenever the date in DATS is contained in FOMCDATES AND time is 14:30 (EDIT: FOMC is 14:00, used 14:30 by mistake - example still valid).
I would like to record the Close before the event takes place in a separate variable. The name of the variable should be based on the date of the event. In the case at hand, the result should be: PreEvent-2016-01-27 = 1122.7. Please take into account this would actually be run in a large sample with dozens of dates and the time can be other than 14:30 (e.g. if looking at NFP rather than FOMC).
DATS <- structure(list(DateTime = structure(list(sec = c(0, 0, 0, 0,0, 0, 0, 0, 0), min = c(30L, 15L, 0L, 45L, 30L, 15L, 0L, 45L,30L), hour = c(15L, 15L, 15L, 14L, 14L, 14L, 14L, 13L, 13L),mday = c(27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L), mon = c(0L,0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), year = c(116L, 116L, 116L,116L, 116L, 116L, 116L, 116L, 116L), wday = c(3L, 3L, 3L,3L, 3L, 3L, 3L, 3L, 3L), yday = c(26L, 26L, 26L, 26L, 26L,26L, 26L, 26L, 26L), isdst = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,0L, 0L), zone = c("EST", "EST", "EST", "EST", "EST", "EST","EST", "EST", "EST"), gmtoff = c(NA_integer_, NA_integer_,NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_,NA_integer_, NA_integer_)), .Names = c("sec", "min", "hour","mday", "mon", "year", "wday", "yday", "isdst", "zone", "gmtoff"), class = c("POSIXlt", "POSIXt")), Close = c(1127.2, 1127.5,1126.9, 1128.3, 1125.4, 1122.7, 1122.8, 1117.3, 1116)), .Names = c("DateTime","Close"), row.names = 2131:2139, class = "data.frame")
FOMCDATES <- structure(c(16785, 16827, 16876), class = "Date")
DATS$Time <- strftime(DATS$DateTime, format="%H:%M:%S")
DATS$Date <- as.Date(DATS$DateTime)
DATS$FOMCBinary <- ifelse( DATS$Time == "14:30:00" & DATS$Date %in% FOMCDATES, 1, 0)
#Output for FOMCDATES:
[1] 2015-12-16 2016-01-27 2016-03-16
#Output for DATS after calculations performed:
DateTime Close Time Date FOMCBinary
2131 2016-01-27 15:30:00 1127.2 15:30:00 2016-01-27 0
2132 2016-01-27 15:15:00 1127.5 15:15:00 2016-01-27 0
2133 2016-01-27 15:00:00 1126.9 15:00:00 2016-01-27 0
2134 2016-01-27 14:45:00 1128.3 14:45:00 2016-01-27 0
2135 2016-01-27 14:30:00 1125.4 14:30:00 2016-01-27 1
2136 2016-01-27 14:15:00 1122.7 14:15:00 2016-01-27 0
2137 2016-01-27 14:00:00 1122.8 14:00:00 2016-01-27 0
2138 2016-01-27 13:45:00 1117.3 13:45:00 2016-01-27 0
2139 2016-01-27 13:30:00 1116.0 13:30:00 2016-01-27 0
My attempt results in a vector rather than a single value, and the variable name is not dynamic.
#My failed attempt
#Define rowShift function
rowShift <- function(x, shiftLen = 1L) {
r <- (1L + shiftLen):(length(x) + shiftLen)
r[r<1] <- NA
return(x[r]) }
PreEventLevel <- ifelse(DATS$FOMCBinary > 0, rowShift(DATS$Close, +1), 0)
How could this be achieved?
Thank you very much!
Creating variables in the global environment with dynamic names is not a good practice... I would rather use a list as container for your values e.g. :
# get the indexes where FOMCBinary > 0
oneIdxs <- which(DATS$FOMCBinary > 0)
# get the close values using indexes on the shifted vector and put the values in a list
PreEventLevel <- as.list(rowShift(DATS$Close,1)[oneIdxs])
# set the dates as names of the element in the list
names(PreEventLevel) <- DATS$Date[oneIdxs]
> PreEventLevel
$`2016-01-27`
[1] 1122.7
# now you can access to values using:
# PreEventLevel[["2016-01-27"]]
# or
# PreEventLevel$`2016-01-27`
Note that you can also simply create a vector with names instead of a list (just remove as.list), and PreEventLevel will be:
> PreEventLevel
2016-01-27
1122.7
# you can access to values using PreEventLevel["2016-01-27"]

how to transform and truncate time stamps from starting time using lubridate?

I want to transforming the time stamps of a dataset into something more workable, and am new to lubridate/R.
The data originally came in %d/%m/%Y %H:%M and would like the change this to %d %H from the starting time.
I want to truncate the data down to DMY_H then rearrange it to Hours and Days after origin
Here is sample of the data:
time dht22_t dht11_t dht22_h dht11_h db pa treatment_hive wifi
01/09/2014 15:19 NA NA NA NA 51.75467 NA 0 1
01/09/2014 15:20 30.8 31 59.8 44 55.27682 100672 0 1
01/09/2014 15:21 30.8 31 60.3 44 54.81995 100675 0 1
01/09/2014 15:22 30.8 31 60.9 44 54.14134 100671 0 1
01/09/2014 15:23 30.8 31 61.1 44 53.88574 100672 0 1
01/09/2014 15:24 30.8 31 61.2 44 53.68800 100680 0 1
Thanks!
EDIT:
The code:
df$time<-format(ymd_hms(df$time), '%d %H:%M:%S')
changed the days, but not the hours minutes seconds to a starting time or origin.
Ideally, it would look like this:
time DHT22_t DHT11_t DHT22_h DHT11_h db pa hive_id treatment_hive wifi
01 15:00:00 NA NA NA NA 51.75467 NA 1 0 1
01 16:00:00 30.8 31 59.8 44 55.27682 100.672 1 0 1
01 17:00:00 30.8 31 60.3 44 54.81995 100.675 1 0 1
EDIT:
> dput(droplevels(head(Hive2)))
structure(list(time = structure(1:6, .Label = c("2014-09-01 15:25:05",
"2014-09-01 15:25:09", "2014-09-01 15:25:11", "2014-09-01 15:25:15",
"2014-09-01 15:25:18", "2014-09-01 15:25:20"), class = "factor"),
DHT22_t = c(0, 0, 0, 0, 0, 0), DHT11_t = c(0L, 31L, 31L,
31L, 31L, 31L), DHT22_h = c(0, 0, 0, 0, 0, 0), DHT11_h = c(0L,
51L, 53L, 53L, 52L, 50L), db = c(60.8, 59.4, 60.4, 59.2,
60.3, 60.2), kPa = c(NA, 100.798, 100.792, 100.791, 100.79,
100.791), hive_id = c(2L, 2L, 2L, 2L, 2L, 2L), treatment_hive = c(1L,
1L, 1L, 1L, 1L, 1L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time",
"DHT22_t", "DHT11_t", "DHT22_h", "DHT11_h", "db", "kPa", "hive_id",
"treatment_hive", "wifi"), row.names = c(NA, 6L), class = "data.frame")
for this code:
dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
dt2<- paste(format(dt1, '%d'), v1-v1[1])
> dput(head(dt2))
c("01 0.000000e+00", "01 4.629630e-05", "01 6.944444e-05",
"01 1.157407e-04", "01 1.504630e-04", "01 1.736111e-04")
and this:
dt2.1<- paste(format(dt1, '%d %H:%M:%S'), v1-v1[1])
> dput(head(dt2.1))
c("01 15:25:05 0.000000e+00", "01 15:25:09 4.629630e-05", "01 15:25:11 6.944444e-05",
"01 15:25:15 1.157407e-04", "01 15:25:18 1.504630e-04", "01 15:25:20 1.736111e-04"
)
You could try
library(lubridate)
library(chron)
dt1 <- dmy_hm(df$time)
NOTE: Based on the example provided, the times are 15:19:00, 15:20:00. One reason to show dataset using dput.
dt1
#[1] "2014-09-01 15:19:00 UTC" "2014-09-01 15:20:00 UTC"
#[3] "2014-09-01 15:21:00 UTC" "2014-09-01 15:22:00 UTC"
#[5] "2014-09-01 15:23:00 UTC" "2014-09-01 15:24:00 UTC"
v1 <- times(format(dt1, '%H:%M:%S'))
paste(format(dt1, '%d'), v1-v1[1])
#[1] "01 00:00:00" "01 00:01:00" "01 00:02:00" "01 00:03:00" "01 00:04:00"
#[6] "01 00:05:00"
Update
Based on the updated dataset "Hive2"
dt1 <- ymd_hms(Hive2$time)
v1 <- times(format(dt1, '%H:%M:%S'))
v1
#[1] 15:25:05 15:25:09 15:25:11 15:25:15 15:25:18 15:25:20
paste(format(dt1, '%d'), v1-v1[1])
#[1] "01 00:00:00" "01 00:00:04" "01 00:00:06" "01 00:00:10" "01 00:00:13"
#[6] "01 00:00:15"
data
df <- structure(list(time = c("01/09/2014 15:19", "01/09/2014 15:20",
"01/09/2014 15:21", "01/09/2014 15:22", "01/09/2014 15:23", "01/09/2014 15:24"
), dht22_t = c(NA, 30.8, 30.8, 30.8, 30.8, 30.8), dht11_t = c(NA,
31L, 31L, 31L, 31L, 31L), dht22_h = c(NA, 59.8, 60.3, 60.9, 61.1,
61.2), dht11_h = c(NA, 44L, 44L, 44L, 44L, 44L), db = c(51.75467,
55.27682, 54.81995, 54.14134, 53.88574, 53.688), pa = c(NA, 100672L,
100675L, 100671L, 100672L, 100680L), treatment_hive = c(0L, 0L,
0L, 0L, 0L, 0L), wifi = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("time",
"dht22_t", "dht11_t", "dht22_h", "dht11_h", "db", "pa", "treatment_hive",
"wifi"), class = "data.frame", row.names = c(NA, -6L))

create new variable from date data

Now my data frame is like below
dput(head(t.zoo))
structure(c(85.92, 85.85, 85.83, 85.83, 85.85, 85.87, 1300, 1300,
1299.75, 1299.75, 1299.75, 1300), .Dim = c(6L, 2L), .Dimnames = list(
NULL, c("cl", "es")), index = structure(list(sec = c(0.400000095367432,
0.900000095367432, 1.40000009536743, 1.90000009536743, 2.40000009536743,
2.90000009536743), min = c(30L, 30L, 30L, 30L, 30L, 30L), hour = c(10L,
10L, 10L, 10L, 10L, 10L), mday = c(6L, 6L, 6L, 6L, 6L, 6L), mon = c(5L,
5L, 5L, 5L, 5L, 5L), year = c(112L, 112L, 112L, 112L, 112L, 112L
), wday = c(3L, 3L, 3L, 3L, 3L, 3L), yday = c(157L, 157L, 157L,
157L, 157L, 157L), isdst = c(1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("sec",
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst"
), class = c("POSIXlt", "POSIXt"), tzone = c("", "EST", "EDT"
)), class = "zoo")
I have two questions, first is I would like to add a variable name for the first column and 2nd is i want to create a categorical variable to help me indicate 2010-06-06 (since there are 3 separate days)
What I should do for the date data?
I'm not familiar with zoo class, so the following code is not nice, but seems working.
yourdata<-as.matrix(yourdata)
justdate <- substr(rownames(yourdata), 1, 10)
justtime <- substr(rownames(yourdata), 11, 19)
row.names(yourdata) <- NULL
yourdata<-as.data.frame(yourdata)
yourdata[,"justdate"]<-justdate
yourdata[,"justtime"]<-justtime
yourdata[yourdata$justdate=="2012-06-06","newvariable"]<-1
> yourdata
cl es justdate justtime newvariable
1 85.92 1300.00 2012-06-06 10:30:00 1
2 85.85 1300.00 2012-06-06 10:30:00 1
3 85.83 1299.75 2012-06-06 10:30:01 1
4 85.83 1299.75 2012-06-06 10:30:01 1
5 85.85 1299.75 2012-06-06 10:30:02 1
6 85.87 1300.00 2012-06-06 10:30:02 1
zoo objects are a little bit different to work with from data.frames.
The "first column" (as you referred to it) is actually not a column, but the index of your object. Try index(t.zoo) and see what it returns. This index really should have unique values; in your case, there are duplicated values, which might affect your calculations.
Conversion to a data.frame can be done like the following. I've added separate "Date" and "Time" variables based on the index from t.zoo.
require(zoo) # Load the `zoo` package if you haven't already done so
t.df = data.frame(Date = format(index(t.zoo), "%Y-%m-%d"),
Time = format(index(t.zoo), "%H:%M:%S"),
data.frame(t.zoo))
t.df
# Date Time cl es
# 1 2012-06-06 10:30:00 85.92 1300.00
# 2 2012-06-06 10:30:00 85.85 1300.00
# 3 2012-06-06 10:30:01 85.83 1299.75
# 4 2012-06-06 10:30:01 85.83 1299.75
# 5 2012-06-06 10:30:02 85.85 1299.75
# 6 2012-06-06 10:30:02 85.87 1300.00
Converting back to a zoo object (keeping the new "Date" and "Time" columns, or any other columns that you have added) can be done like:
zoo(t.df, order.by=index(t.zoo))
Note, however, that this will give you a warning because you don't have unique "order.by" values.

Resources