[R] Mean of dates

Jue Lin-Ye jl.iccp at gmail.com
Mon Apr 27 11:26:14 CEST 2015


On 24 April 2015 at 13:47, Jue Lin-Ye <jl.iccp at gmail.com> wrote:

>
>
> On 24 April 2015 at 12:59, Achim Zeileis <Achim.Zeileis at uibk.ac.at> wrote:
>
>>
>>
>> On Fri, 24 Apr 2015, Jue Lin-Ye wrote:
>>
>>  Dear fellow R-help members,
>>>
>>> If my data is
>>>
>>> YYYY MM DD HH
>>> 2015 04 24 01
>>> 2015 04 24 02
>>> 2015 04 24 06
>>>
>>> Where
>>>
>>> YYYY: year
>>> MM:month
>>> DD:day
>>> HH: hour
>>>
>>> How could I calculate the mean of the ISOdatetime(YYYY,MM,DD,HH,0,0) of
>>> these?
>>>
>>
>> With the mean() method? On my machine:
>>
>> R> mean(ISOdatetime(YYYY,MM,DD,HH,0,0))
>> [1] "2015-04-24 03:00:00 CEST"
>>
>>
> ​Hi! Maybe the problem is when I try to create the vector that I am
> looking for. Please ​check this code out.
>
>
> ​X_season1<-matrix(c(2015, 04, 24, 01, 2015 ,04, 24, 02,
>   2015 ,04 ,24 ,03 ,2015 ,04 ,24 ,05,
>   2015, 04, 24 ,06 ,2015, 04 ,24
> ,10),3,8,byrow=T);colnames(X_season1)<-c("AAi","MMi","DDi","HHi","AAf","MMf","DDf","HHf")
> time2<-NULL
> for(i5 in 1:nrow(X_season1)){
>   time2[i5]<-mean(
>
> as.Date(c(ISOdatetime(X_season1[i5,"AAi"],X_season1[i5,"MMi"],X_season1[i5,"DDi"],X_season1[i5,"HHi"],0,0),
>
> ISOdatetime(X_season1[i5,"AAf"],X_season1[i5,"MMf"],X_season1[i5,"DDf"],X_season1[i5,"HHf"],0,0)),
>                           format="%YYYY-%mm-%dd %H:%m:%s"),trim=0)}​
>
> ​Thanks!​
>
>
Dear Dr. Zeileis,

 Here is the solution that I finally came up with, I hope you agree with
it. From your suggestion, I saw that the problem was the creation of the
vector. It needed to be numeric. So I calculate the difftime from each date
to a reference date (1990/01/01 00:00:00). The units is "hours". I obtain a
vector of difference numbers. Then I add these numbers, transformed into
seconds, to the reference date. ​


​Thank you so much for your help!

Best regards,

Jue​


>
>> hth,
>> Z
>>
>>  Note: I set minutes and seconds to 0, as I don't have data for them.
>>>
>>> ?Thank you in advance!?
>>>
>>> --
>>> Jue Lin-Ye
>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>>
>>
>
>
>
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list