[R] time in year, month, day, hour ?

Joshua Wiley jwiley.psych at gmail.com
Thu Sep 30 22:12:28 CEST 2010


Dear Yogesh,


This will create a vector that I believe does what you want.

x <- as.POSIXct(x = cumsum(c(0, rep(3*60*60, 1919))), origin = "2009-01-01")

Let me see if I can explain the logic.  In the innermost part, I
multiply 3*60*60, or hours*minutes*seconds.  You said they were three
hour blocks so that is why I used three.  This is because POSIXct,
which I think is the format you will want to use, measures things in
number of seconds since the origin (baseline point).  Next, I repeat
that number of seconds 1,919 times, and combine it with a 0 at the
beginning.  This creates a vector of length 1920 with 0, and then
number of seconds in 3 hours.  Next find the cumulative sum (this gets
time moving forwards).  Finally specify the origin (traditionally in R
"1970-01-01"), but we know you want it to be "2009-01-01".  The
results from this are assigned to "x".

Hope that helps,

Josh

On Thu, Sep 30, 2010 at 8:59 AM, Yogesh Tiwari
<yogesh.mpi at googlemail.com> wrote:
> Dear R Users,
> I did not get any reply on my question so I am re-asking.
>
> This time I am giving sample data:
>
>     1        60.3162  -13.5993   -0.4353   46.0938    0.1877  -0.194E-07
>     2        60.3713  -13.5992   -0.4423   46.1241    0.2057  -0.231E-06
>     3        60.3430  -13.5981   -1.6163   44.9048    0.2237  -0.270E-06
>     4        60.3227  -13.5970   -2.6258   43.8785    0.2213  -0.139E-06
>     5        60.3352  -13.5961   -2.5874   43.9238    0.2278   0.141E-06
> .......
> .....
> ......
>  1918      59.1785  -14.5851    0.3895   44.9850   -0.0021   0.141E-06
>  1919      59.1816  -14.5622    0.3933   44.9972    0.0155   0.139E-06
>  1920      59.1637  -14.5666    0.4420   45.0219    0.0172   0.138E-06
>
> Column 1, is index of hours on 3 hourly interval and starts from 1 Jan 2009,
> 00 hrs and run till 240 days of  2009.
> I want to convert Column 1 of above data in the format, 'year' , 'month',
> 'day',  'hour'
>
> Kindly help.
>
> Thanks,
>
> Regards,
> Yogesh
>
> My question is below
>
> Dear R Users,
>>
>> I have model simulated data for 240 days.
>>
>> The day 1 is Jan 1, 2009, 00:00 hrs and then with 3-hourly interval and so
>> on.
>>
>> The time axis is 1,2,3,4......1920;   so the total rows in the data are
>> 1920.
>>
>> How to convert above time axis in  "year" "month" "day" "hour" format
>>
>> Great Thanks,
>>
>> regards,
>> Yogesh
>>
>>
>>
>
>
> --
> Yogesh K. Tiwari (Dr.rer.nat),
> Scientist,
> Centre for Climate Change Research,
> Indian Institute of Tropical Meteorology,
> Homi Bhabha Road,
> Pashan,
> Pune-411008
> INDIA
>
> Phone: 0091-99 2273 9513 (Cell)
>         : 0091-20-25904452 (O)
> Fax    : 0091-20-258 93 825
>
>        [[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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list