ddply with POSIXct (vs. Date)

167 views
Skip to first unread message

Joseph Voelkel

unread,
Oct 23, 2013, 8:50:14 PM10/23/13
to manip...@googlegroups.com

ddply seems to have difficulty with POSIXct. Here is an example (based

on my initial problem, which was using ddply to get ranges of

dates based on a grouping variable. I have omitted a grouping variable here)

 

try1<-structure(c(-2208988800, -2206310400, -2203891200, -2201212800,

-999475200, -996796800, -994204800, -991526400), class = c("POSIXct",

"POSIXt"), tzone = "UTC")

 

try1 # all whole dates

 

# Like try1, but 10-second change to first element

try1a<-structure(c(-2208988800-10, -2206310400, -2203891200, -2201212800,

-999475200, -996796800, -994204800, -991526400), class = c("POSIXct",

"POSIXt"), tzone = "UTC")

 

try1a[1:4] # 10 second change in first element...

 

# Like try1, but different class

try2<-structure(c(-25567, -25536, -25508, -25477, -11568, -11537, -11507,

-11476), class = "Date")

 

all.equal(as.Date(try1),try2)

 

# put into data frames for ddply use

x1<-data.frame(try1)

x1a<-data.frame(try1a)

x2<-data.frame(try2)

 

# base R results. These are correct

range(x1$try1)

range(x1a$try1a) # 10 second diff. 1st element is the minimum

range(x2$try2)

 

# But ...

ddply(x1,.(),function(dfr) range(dfr[,1])) # off by 5 or 4 hours

ddply(x1a,.(),function(dfr) range(dfr[,1])) # delta of 10 secs in min, so delta is correct

ddply(x2,.(),function(dfr) range(dfr[,1])) # OK

 

# as.Date "corrects" this

ddply(x1,.(),function(dfr) range(as.Date(dfr[,1])))

 

# and see individual returns values

(test1<-ddply(x1,.(),function(dfr) dfr[1]) )

all.equal(x1,test1[2]) # OK

# But this does not work

ddply(x1,.(),function(dfr) dfr[,1]) # off by 5 or 4 hours

 

Ideas?

 

Joe Voelkel

 

Hadley Wickham

unread,
Oct 24, 2013, 8:36:55 AM10/24/13
to Joseph Voelkel, manip...@googlegroups.com
I think this succinctly demonstrates the problem:

try1<-structure(c(-2208988800, -2206310400, -2203891200, -2201212800,
-999475200, -996796800, -994204800, -991526400), class = c("POSIXct",
"POSIXt"), tzone = "UTC")
x1<-data.frame(try1)

dlply(x1,.(),function(dfr) range(dfr[,1])) # OK
ddply(x1,.(),function(dfr) range(dfr[,1])) # NOT OK


But this sort of problem is notoriously painful in R because of how
easy it is to lose the timezone attribute.

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "manipulatr" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to manipulatr+...@googlegroups.com.
> To post to this group, send email to manip...@googlegroups.com.
> Visit this group at http://groups.google.com/group/manipulatr.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Chief Scientist, RStudio
http://had.co.nz/

Joseph Voelkel

unread,
Oct 24, 2013, 9:46:31 AM10/24/13
to Hadley Wickham, manip...@googlegroups.com
Aha! Thanks, Hadley.

Joe
Reply all
Reply to author
Forward
0 new messages