Duncan,

thanks very much -- this explains the behaviour.

Also, if we make days a list, the class attributes are kept when looping
over the list, ie.

days<- list( as.Date( c("2000-01-01", "2000-01-02") ) )

for (day in days) {
# class(day) <- class(days)
 print(as.POSIXct(day))
}

works as expected. I suspect I shall look into the coercing (and subsetting)
rules to find out why this is happening.



On Fri, Jul 15, 2011 at 6:48 PM, Duncan Murdoch <murdoch.dun...@gmail.com>wrote:

> On 15/07/2011 12:15 PM, Johannes Egner wrote:
>
>> Dear all,
>>
>> how come the first loop in the below fails, but the second performs as
>> expected?
>>
>> days<- as.Date( c("2000-01-01", "2000-01-02") )
>>
>> for(day in days)
>> {
>>     as.POSIXct(day)
>> }
>>
>
> "day" in the loop above is an integer without a class, it's not a Date.  If
> you did
>
> for (day in days) {
>  class(day) <- class(days)
>  print(as.POSIXct(day))
> }
>
> you won't get an error.  (I don't know if you'll be happy with what you
> get; the time zone is an issue.)
>
> Duncan Murdoch
>
>  for( n in 1:length(days) )
>> {
>>     show(as.POSIXct(days[n]))
>> }
>>
>> Many thanks, Jo
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________**________________
>> R-help@r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[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