0

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?

user1489975
  • 1,841
  • 2
  • 14
  • 8
  • 2
    Please use `dput` to create a sample of your data. And read this: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ari B. Friedman Jul 13 '12 at 14:36
  • The "first column" looks like rownames or the index of an xts/zoo object. You really need to provide the output from `dput` for anyone to do more than guess at solutions. – Joshua Ulrich Jul 13 '12 at 15:02
  • thanks for ur comment! I would put the dput and try to make it work – user1489975 Jul 13 '12 at 15:47
  • @user1489975, how did either of these answers work out for you? If neither did, please refine your question so others can try to help answer it. – A5C1D2H2I1M1N2O1R2T1 Dec 06 '12 at 09:53

2 Answers2

0

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  
sztup
  • 225
  • 1
  • 2
  • 7
0

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.

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485