numbers loop in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

numbers loop in R

emj83
I would like to create a matrix in R that looks similar to this:

     [,1] [,2] [,3] [,4]
[1,]  NaN  1  2  3
[2,]  NaN  1  2  4
[3,]  NaN   1  2  5
[4,]  NaN  2  3  4
[5,]  NaN  2  3  5
[6,]  NaN    3    4    5

I have the loop below:

where A for example is 5

matrixx<-function(A){
B=matrix(NaN,nrow=(A+1),ncol=4)
        for(k in 1:(A+1)){
                for(i in 1:(A-2)){
                     for(j in (i+2):A){
                     }
                }
       }
B[k,]=c(NaN,i,(i+1),j)
print(B)
}

But it only prints the final line in:

> matrixx(5)
     [,1] [,2] [,3] [,4]
[1,]  NaN  NaN  NaN  NaN
[2,]  NaN  NaN  NaN  NaN
[3,]  NaN  NaN  NaN  NaN
[4,]  NaN  NaN  NaN  NaN
[5,]  NaN  NaN  NaN  NaN
[6,]  NaN    3    4    5

Could anyone give me a hand? Would be much appreciated.

Thanks Emma
Reply | Threaded
Open this post in threaded view
|

Re: numbers loop in R

David Winsemius
I would have expected to see the assignment to B[k,] inside the loops.  
And to see some connection with the k index in the inner loops if you  
did not want all of the rows to be similar. Because the assignment is  
outside the loops, it happens only once.

-- David Winsemius


On Apr 17, 2009, at 11:11 AM, emj83 wrote:

>
> I would like to create a matrix in R that looks similar to this:
>
>     [,1] [,2] [,3] [,4]
> [1,]  NaN  1  2  3
> [2,]  NaN  1  2  4
> [3,]  NaN   1  2  5
> [4,]  NaN  2  3  4
> [5,]  NaN  2  3  5
> [6,]  NaN    3    4    5
>
> I have the loop below:
>
> where A for example is 5
>
> matrixx<-function(A){
> B=matrix(NaN,nrow=(A+1),ncol=4)
>        for(k in 1:(A+1)){
>        for(i in 1:(A-2)){
>     for(j in (i+2):A){
>     }
> }
>       }
> B[k,]=c(NaN,i,(i+1),j)
> print(B)
> }
>
> But it only prints the final line in:
>
>> matrixx(5)
>     [,1] [,2] [,3] [,4]
> [1,]  NaN  NaN  NaN  NaN
> [2,]  NaN  NaN  NaN  NaN
> [3,]  NaN  NaN  NaN  NaN
> [4,]  NaN  NaN  NaN  NaN
> [5,]  NaN  NaN  NaN  NaN
> [6,]  NaN    3    4    5
>
> Could anyone give me a hand? Would be much appreciated.
>
> Thanks Emma
>
> --
> View this message in context: http://www.nabble.com/numbers-loop-in-R-tp23099591p23099591.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: numbers loop in R

jholtman
In reply to this post by emj83
try this:

> matrixx<-function(A){
+     B=matrix(NaN,nrow=(A+1),ncol=4)
+     k <- 1
+     for (i in 3:A){
+         for (j in i:A) {
+             B[k,] <- c(NaN, i-2, i-1, j)
+             k <- k + 1
+         }
+     }
+     B
+ }
> matrixx(5)
     [,1] [,2] [,3] [,4]
[1,]  NaN    1    2    3
[2,]  NaN    1    2    4
[3,]  NaN    1    2    5
[4,]  NaN    2    3    4
[5,]  NaN    2    3    5
[6,]  NaN    3    4    5
>


On Fri, Apr 17, 2009 at 11:11 AM, emj83 <[hidden email]> wrote:

>
> I would like to create a matrix in R that looks similar to this:
>
>     [,1] [,2] [,3] [,4]
> [1,]  NaN  1  2  3
> [2,]  NaN  1  2  4
> [3,]  NaN   1  2  5
> [4,]  NaN  2  3  4
> [5,]  NaN  2  3  5
> [6,]  NaN    3    4    5
>
> I have the loop below:
>
> where A for example is 5
>
> matrixx<-function(A){
> B=matrix(NaN,nrow=(A+1),ncol=4)
>        for(k in 1:(A+1)){
>                for(i in 1:(A-2)){
>                     for(j in (i+2):A){
>                     }
>                }
>       }
> B[k,]=c(NaN,i,(i+1),j)
> print(B)
> }
>
> But it only prints the final line in:
>
>> matrixx(5)
>     [,1] [,2] [,3] [,4]
> [1,]  NaN  NaN  NaN  NaN
> [2,]  NaN  NaN  NaN  NaN
> [3,]  NaN  NaN  NaN  NaN
> [4,]  NaN  NaN  NaN  NaN
> [5,]  NaN  NaN  NaN  NaN
> [6,]  NaN    3    4    5
>
> Could anyone give me a hand? Would be much appreciated.
>
> Thanks Emma
>
> --
> View this message in context: http://www.nabble.com/numbers-loop-in-R-tp23099591p23099591.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: numbers loop in R

hadley wickham
On Fri, Apr 17, 2009 at 12:19 PM, jim holtman <[hidden email]> wrote:

> try this:
>
>> matrixx<-function(A){
> +     B=matrix(NaN,nrow=(A+1),ncol=4)
> +     k <- 1
> +     for (i in 3:A){
> +         for (j in i:A) {
> +             B[k,] <- c(NaN, i-2, i-1, j)
> +             k <- k + 1
> +         }
> +     }
> +     B
> + }
>> matrixx(5)
>     [,1] [,2] [,3] [,4]
> [1,]  NaN    1    2    3
> [2,]  NaN    1    2    4
> [3,]  NaN    1    2    5
> [4,]  NaN    2    3    4
> [5,]  NaN    2    3    5
> [6,]  NaN    3    4    5

Here's a solution without the loop.  I think it illustrates the intent
of the algorithm more clearly.

candidates <- t(combn(5,3))
firstdiff <- candidates[,2] - candidates[, 1]
cbind(NaN, candidates[firstdiff == 1, ])

Hadley

--
http://had.co.nz/

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

end of daylight saving time

Denis Chabot
Hi,

I read from the help on DateTimeClasses and various posts on this list  
that, quite logically, one needs to specify if DST is in function for  
the first hour  after the change from DST to ST in autumn.

Hence, in my time zone and on Mac OS X, I can do this:

a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
automatic use of DST
b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this is  
the second occurrence of 1:30 that day, in ST
difftime(b,a)

But why can't I do this, to handle several date-times at once?

c <- rep("2008-11-02 01:30:00", 2)
tzone = c("EST5EDT", "EST")

as.POSIXct(c, tz=tzone)
Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
   valeur 'tz' incorrecte

???

Thanks,

Denis Chabot

sessionInfo()
R version 2.9.1 Patched (2009-07-09 r48929)
x86_64-apple-darwin9.7.0

locale:
fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.9.1

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

problem with as.POSIXct and daylight savings time

Denis Chabot
In reply to this post by hadley wickham
[was " [R] end of daylight saving time"]

Hi,

I got no reply with the previous subject line, probably a bad choice  
of subject on my part, so here it is again.

I read from the help on DateTimeClasses and various posts on this list  
that, quite logically, one needs to specify if DST is active or not  
when time is between 1 and 2 AM on the first Sunday in November (for  
North America in recent years).

This I can do for on date at a time:

a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
automatic use of DST
b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this is  
the second occurrence of 1:30 that day, in ST
difftime(b,a)

Time difference of 1 hours

But why can't I do the following, which appears to be a typical R way  
of doing things, to handle several date-times at once?

c <- rep("2008-11-02 01:30:00", 2)
tzone = c("EST5EDT", "EST")

as.POSIXct(c, tz=tzone)
Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
  valeur 'tz' incorrecte

???

Thanks,

Denis Chabot

sessionInfo()
R version 2.9.1 Patched (2009-07-09 r48929)
x86_64-apple-darwin9.7.0

locale:
fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.9.1

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

problem with as.POSIXct and daylight savings time

Denis Chabot
In reply to this post by hadley wickham
[was " [R] end of daylight saving time"]

Hi,

I got no reply with the previous subject line, probably a bad choice  
of subject on my part, so here it is again.

I read from the help on DateTimeClasses and various posts on this list  
that, quite logically, one needs to specify if DST is active or not  
when time is between 1 and 2 AM on the first Sunday in November (for  
North America in recent years).

This I can do for on date at a time:

a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
automatic use of DST
b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this is  
the second occurrence of 1:30 that day, in ST
difftime(b,a)

Time difference of 1 hours

But why can't I do the following, which appears to be a typical R way  
of doing things, to handle several date-times at once?

c <- rep("2008-11-02 01:30:00", 2)
tzone = c("EST5EDT", "EST")

as.POSIXct(c, tz=tzone)
Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
  valeur 'tz' incorrecte

???

Thanks,

Denis Chabot

sessionInfo()
R version 2.9.1 Patched (2009-07-09 r48929)
x86_64-apple-darwin9.7.0

locale:
fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.9.1

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: problem with as.POSIXct and daylight savings time

Duncan Murdoch
On 19/07/2009 11:23 AM, Denis Chabot wrote:

> [was " [R] end of daylight saving time"]
>
> Hi,
>
> I got no reply with the previous subject line, probably a bad choice  
> of subject on my part, so here it is again.
>
> I read from the help on DateTimeClasses and various posts on this list  
> that, quite logically, one needs to specify if DST is active or not  
> when time is between 1 and 2 AM on the first Sunday in November (for  
> North America in recent years).
>
> This I can do for on date at a time:
>
> a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
> automatic use of DST
> b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this is  
> the second occurrence of 1:30 that day, in ST
> difftime(b,a)
>
> Time difference of 1 hours
>
> But why can't I do the following, which appears to be a typical R way  
> of doing things, to handle several date-times at once?
>
> c <- rep("2008-11-02 01:30:00", 2)
> tzone = c("EST5EDT", "EST")
>
> as.POSIXct(c, tz=tzone)
> Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
>   valeur 'tz' incorrecte
>
> ???

Objects of the POSIXlt and POSIXct classes don't support multiple time
zones, so if you specified several time zones on input, how would the
conversion functions decide which one to use for output?  You'll need to
write your own wrapper function to make this decision, and do the
conversions separately for each input timezone.

Why don't those classes support a separate time zone for each entry?
Presumably because their designer never thought anyone would want to do
that.

Duncan Murdoch


>
> Thanks,
>
> Denis Chabot
>
> sessionInfo()
> R version 2.9.1 Patched (2009-07-09 r48929)
> x86_64-apple-darwin9.7.0
>
> locale:
> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_2.9.1
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: problem with as.POSIXct and daylight savings time

Denis Chabot
Thank you very much Duncan.

I'll follow your suggestion.

Why do I want to do what the designer did not think anyone would want  
to do? I have data acquisition equipment taking measurements every 15  
min or so for days at a time, and I need to compile all such  
experiments in a master data set. The data acquisition equipment  
automatically switches to DST in spring and back to ST in autumn,  
which I did not disable because it is easier to work with while we are  
running the experiments.

I could use chron to ignore time zones and daylight savings time, but  
this would not be of much help as whether or not I use as.POSIXct or  
chron, there is one day of the year that has 25 h and I need to deal  
with that 25th hour or I'll lose one hour of data!

Denis
Le 09-07-19 à 11:45, Duncan Murdoch a écrit :

> On 19/07/2009 11:23 AM, Denis Chabot wrote:
>> [was " [R] end of daylight saving time"]
>> Hi,
>> I got no reply with the previous subject line, probably a bad  
>> choice  of subject on my part, so here it is again.
>> I read from the help on DateTimeClasses and various posts on this  
>> list  that, quite logically, one needs to specify if DST is active  
>> or not  when time is between 1 and 2 AM on the first Sunday in  
>> November (for  North America in recent years).
>> This I can do for on date at a time:
>> a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
>> automatic use of DST
>> b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this  
>> is  the second occurrence of 1:30 that day, in ST
>> difftime(b,a)
>> Time difference of 1 hours
>> But why can't I do the following, which appears to be a typical R  
>> way  of doing things, to handle several date-times at once?
>> c <- rep("2008-11-02 01:30:00", 2)
>> tzone = c("EST5EDT", "EST")
>> as.POSIXct(c, tz=tzone)
>> Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
>>  valeur 'tz' incorrecte
>> ???
>
> Objects of the POSIXlt and POSIXct classes don't support multiple  
> time zones, so if you specified several time zones on input, how  
> would the conversion functions decide which one to use for output?  
> You'll need to write your own wrapper function to make this  
> decision, and do the conversions separately for each input timezone.
>
> Why don't those classes support a separate time zone for each entry?  
> Presumably because their designer never thought anyone would want to  
> do that.
>
> Duncan Murdoch
>
>
>> Thanks,
>> Denis Chabot
>> sessionInfo()
>> R version 2.9.1 Patched (2009-07-09 r48929)
>> x86_64-apple-darwin9.7.0
>> locale:
>> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>> loaded via a namespace (and not attached):
>> [1] tools_2.9.1
>> ______________________________________________
>> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: problem with as.POSIXct and daylight savings time

Spencer Graves-3
  Have you considered the "timeDate" package?

      Spencer

Denis Chabot wrote:

> Thank you very much Duncan.
>
> I'll follow your suggestion.
>
> Why do I want to do what the designer did not think anyone would want
> to do? I have data acquisition equipment taking measurements every 15
> min or so for days at a time, and I need to compile all such
> experiments in a master data set. The data acquisition equipment
> automatically switches to DST in spring and back to ST in autumn,
> which I did not disable because it is easier to work with while we are
> running the experiments.
>
> I could use chron to ignore time zones and daylight savings time, but
> this would not be of much help as whether or not I use as.POSIXct or
> chron, there is one day of the year that has 25 h and I need to deal
> with that 25th hour or I'll lose one hour of data!
>
> Denis
> Le 09-07-19 à 11:45, Duncan Murdoch a écrit :
>
>> On 19/07/2009 11:23 AM, Denis Chabot wrote:
>>> [was "    [R] end of daylight saving time"]
>>> Hi,
>>> I got no reply with the previous subject line, probably a bad
>>> choice  of subject on my part, so here it is again.
>>> I read from the help on DateTimeClasses and various posts on this
>>> list  that, quite logically, one needs to specify if DST is active
>>> or not  when time is between 1 and 2 AM on the first Sunday in
>>> November (for  North America in recent years).
>>> This I can do for on date at a time:
>>> a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
>>> automatic use of DST
>>> b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this
>>> is  the second occurrence of 1:30 that day, in ST
>>> difftime(b,a)
>>> Time difference of 1 hours
>>> But why can't I do the following, which appears to be a typical R
>>> way  of doing things, to handle several date-times at once?
>>> c <- rep("2008-11-02 01:30:00", 2)
>>> tzone = c("EST5EDT", "EST")
>>> as.POSIXct(c, tz=tzone)
>>> Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
>>>  valeur 'tz' incorrecte
>>> ???
>>
>> Objects of the POSIXlt and POSIXct classes don't support multiple
>> time zones, so if you specified several time zones on input, how
>> would the conversion functions decide which one to use for output?  
>> You'll need to write your own wrapper function to make this decision,
>> and do the conversions separately for each input timezone.
>>
>> Why don't those classes support a separate time zone for each entry?
>> Presumably because their designer never thought anyone would want to
>> do that.
>>
>> Duncan Murdoch
>>
>>
>>> Thanks,
>>> Denis Chabot
>>> sessionInfo()
>>> R version 2.9.1 Patched (2009-07-09 r48929)
>>> x86_64-apple-darwin9.7.0
>>> locale:
>>> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
>>> attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>> loaded via a namespace (and not attached):
>>> [1] tools_2.9.1
>>> ______________________________________________
>>> [hidden email] 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.
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: problem with as.POSIXct and daylight savings time

Denis Chabot
Thanks for the suggestion, Spencer. I will take a look and will report  
to the list if I find this a better solution for my situation. Might  
take a couple of days though.

Denis
Le 09-07-19 à 12:42, spencerg a écrit :

> Have you considered the "timeDate" package?
>     Spencer
>
> Denis Chabot wrote:
>> Thank you very much Duncan.
>>
>> I'll follow your suggestion.
>>
>> Why do I want to do what the designer did not think anyone would  
>> want to do? I have data acquisition equipment taking measurements  
>> every 15 min or so for days at a time, and I need to compile all  
>> such experiments in a master data set. The data acquisition  
>> equipment automatically switches to DST in spring and back to ST in  
>> autumn, which I did not disable because it is easier to work with  
>> while we are running the experiments.
>>
>> I could use chron to ignore time zones and daylight savings time,  
>> but this would not be of much help as whether or not I use  
>> as.POSIXct or chron, there is one day of the year that has 25 h and  
>> I need to deal with that 25th hour or I'll lose one hour of data!
>>
>> Denis
>> Le 09-07-19 à 11:45, Duncan Murdoch a écrit :
>>
>>> On 19/07/2009 11:23 AM, Denis Chabot wrote:
>>>> [was "    [R] end of daylight saving time"]
>>>> Hi,
>>>> I got no reply with the previous subject line, probably a bad  
>>>> choice  of subject on my part, so here it is again.
>>>> I read from the help on DateTimeClasses and various posts on this  
>>>> list  that, quite logically, one needs to specify if DST is  
>>>> active or not  when time is between 1 and 2 AM on the first  
>>>> Sunday in November (for  North America in recent years).
>>>> This I can do for on date at a time:
>>>> a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get  
>>>> automatic use of DST
>>>> b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T  
>>>> this is  the second occurrence of 1:30 that day, in ST
>>>> difftime(b,a)
>>>> Time difference of 1 hours
>>>> But why can't I do the following, which appears to be a typical R  
>>>> way  of doing things, to handle several date-times at once?
>>>> c <- rep("2008-11-02 01:30:00", 2)
>>>> tzone = c("EST5EDT", "EST")
>>>> as.POSIXct(c, tz=tzone)
>>>> Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
>>>> valeur 'tz' incorrecte
>>>> ???
>>>
>>> Objects of the POSIXlt and POSIXct classes don't support multiple  
>>> time zones, so if you specified several time zones on input, how  
>>> would the conversion functions decide which one to use for  
>>> output?  You'll need to write your own wrapper function to make  
>>> this decision, and do the conversions separately for each input  
>>> timezone.
>>>
>>> Why don't those classes support a separate time zone for each  
>>> entry? Presumably because their designer never thought anyone  
>>> would want to do that.
>>>
>>> Duncan Murdoch
>>>
>>>
>>>> Thanks,
>>>> Denis Chabot
>>>> sessionInfo()
>>>> R version 2.9.1 Patched (2009-07-09 r48929)
>>>> x86_64-apple-darwin9.7.0
>>>> locale:
>>>> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
>>>> attached base packages:
>>>> [1] stats     graphics  grDevices utils     datasets  methods    
>>>> base
>>>> loaded via a namespace (and not attached):
>>>> [1] tools_2.9.1
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: problem with as.POSIXct and daylight savings time

Gabor Grothendieck
In reply to this post by Denis Chabot
as.chron.POSIXt has an offset= argument and it may be
a vector:

> args(chron:::as.chron.POSIXt)
function (x, offset = 0, tz = "GMT", ...)

On Sun, Jul 19, 2009 at 9:10 AM, Denis Chabot<[hidden email]> wrote:

> [was "  [R] end of daylight saving time"]
>
> Hi,
>
> I got no reply with the previous subject line, probably a bad choice
> of subject on my part, so here it is again.
>
> I read from the help on DateTimeClasses and various posts on this list
> that, quite logically, one needs to specify if DST is active or not
> when time is between 1 and 2 AM on the first Sunday in November (for
> North America in recent years).
>
> This I can do for on date at a time:
>
> a <- as.POSIXct("2008-11-02 01:30:00", tz="EST5EDT")  # to get
> automatic use of DST
> b <- as.POSIXct("2008-11-02 01:30:00", tz="EST")  # to tell T this is
> the second occurrence of 1:30 that day, in ST
> difftime(b,a)
>
> Time difference of 1 hours
>
> But why can't I do the following, which appears to be a typical R way
> of doing things, to handle several date-times at once?
>
> c <- rep("2008-11-02 01:30:00", 2)
> tzone = c("EST5EDT", "EST")
>
> as.POSIXct(c, tz=tzone)
> Erreur dans strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
>  valeur 'tz' incorrecte
>
> ???
>
> Thanks,
>
> Denis Chabot
>
> sessionInfo()
> R version 2.9.1 Patched (2009-07-09 r48929)
> x86_64-apple-darwin9.7.0
>
> locale:
> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> loaded via a namespace (and not attached):
> [1] tools_2.9.1
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.