Thank you!  That solution worked!  I thought I'd tried something similar to
that, but obviously I didn't.  Here's a self-contained example, for
posterity and completeness:

z.df <- data.frame(times=c(Sys.time(), Sys.time() + 3600))
as.POSIXlt(z.df[,1])$hour

And this gives me what I want.

Thank you again!
Matt

On Fri, Sep 24, 2010 at 5:50 PM, Phil Spector <spec...@stat.berkeley.edu>wrote:

> 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
>                                         spec...@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@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.
>>
>>


-- 
"Pardon him. Theodotus: he is a barbarian, and thinks that the customs of
his tribe and island are the laws of nature."  -- George Bernard Shaw,
"Caesar and Cleopatra"

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to