Matthew -
It's a bit simpler than you think:
as.POSIXlt(pk)$hour
should return what you want. (If not, please provide a
reproducible example.)
- Phil Spector
Statistical Computing Facility
Department of Statistics
UC Berkeley
spector at stat.berkeley.edu
On Fri, 24 Sep 2010, Matthew Pettis wrote:
> Hi,
>
> I have a list/data.frame 'pk' of POSIXct dates, and I'd like to
extract the
> hour for each row. I know that if I have an individual POSIXct object, I
> can extract the hour by converting to a new object with:
>
> new.lt <- as.POSIXlt(<single POSIXct object>)
> new.lt$hour
>
> But I can't figure out how to apply this for a list of such dates in a
> vectorized form. I can write a loop, I guess and implement this, but I
> think I'm missing a way that takes advantage of vectorization. Here is
my
> loop to just print the hour extracts:
>
> for (ct in pk) {
> lt <- as.POSIXlt(ct, origin="1970-01-01")
> print(lt$hour)
> }
>
> So is there a shorter vectorization idiom that lets me do this? I
can't
> figure out how to use 'lapply' to apply the '$' operator...
>
> Thanks,
> Matt
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>