[R] Converting "day of year" to "year", "month" and "day"

peter dalgaard pd@|gd @end|ng |rom gm@||@com
Sat Jan 16 18:24:02 CET 2021


Something like this?

> as.Date(ISOdate(1998,1,1))+(1:100)-1
  [1] "1998-01-01" "1998-01-02" "1998-01-03" "1998-01-04" "1998-01-05"
  [6] "1998-01-06" "1998-01-07" "1998-01-08" "1998-01-09" "1998-01-10"
 [11] "1998-01-11" "1998-01-12" "1998-01-13" "1998-01-14" "1998-01-15"
 [16] "1998-01-16" "1998-01-17" "1998-01-18" "1998-01-19" "1998-01-20"
 [21] "1998-01-21" "1998-01-22" "1998-01-23" "1998-01-24" "1998-01-25"
 [26] "1998-01-26" "1998-01-27" "1998-01-28" "1998-01-29" "1998-01-30"
 [31] "1998-01-31" "1998-02-01" "1998-02-02" "1998-02-03" "1998-02-04"
 [36] "1998-02-05" "1998-02-06" "1998-02-07" "1998-02-08" "1998-02-09"
 [41] "1998-02-10" "1998-02-11" "1998-02-12" "1998-02-13" "1998-02-14"
 [46] "1998-02-15" "1998-02-16" "1998-02-17" "1998-02-18" "1998-02-19"
 [51] "1998-02-20" "1998-02-21" "1998-02-22" "1998-02-23" "1998-02-24"
 [56] "1998-02-25" "1998-02-26" "1998-02-27" "1998-02-28" "1998-03-01"
 [61] "1998-03-02" "1998-03-03" "1998-03-04" "1998-03-05" "1998-03-06"
 [66] "1998-03-07" "1998-03-08" "1998-03-09" "1998-03-10" "1998-03-11"
 [71] "1998-03-12" "1998-03-13" "1998-03-14" "1998-03-15" "1998-03-16"
 [76] "1998-03-17" "1998-03-18" "1998-03-19" "1998-03-20" "1998-03-21"
 [81] "1998-03-22" "1998-03-23" "1998-03-24" "1998-03-25" "1998-03-26"
 [86] "1998-03-27" "1998-03-28" "1998-03-29" "1998-03-30" "1998-03-31"
 [91] "1998-04-01" "1998-04-02" "1998-04-03" "1998-04-04" "1998-04-05"
 [96] "1998-04-06" "1998-04-07" "1998-04-08" "1998-04-09" "1998-04-10"

Or, if you want month numbers and day of month as numerics:

> as.POSIXlt(as.Date(ISOdate(1998,1,1))+(1:100)-1)$mon
  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1
 [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
 [75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3
> as.POSIXlt(as.Date(ISOdate(1998,1,1))+(1:100)-1)$mday
  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 [26] 26 27 28 29 30 31  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19
 [51] 20 21 22 23 24 25 26 27 28  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16
 [76] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  1  2  3  4  5  6  7  8  9 10

-pd

> On 16 Jan 2021, at 07:48 , Jibrin Alhassan <jibrin.alhassan using unn.edu.ng> wrote:
> 
> Hi Barradas
> Sorry for the delay. Below is a section of my data. I have up to 1826
> covering 1998 to 2002
> year   day Hr SWS
> 1998   1  0  344.
> 1998   2  0  346.
> 1998   3  0  356.
> 1998   4  0  332.
> 1998   5  0  302.
> 1998   6  0  329.
> 1998   7  0  395.
> 1998   8  0  359.
> 1998   9  0  471.
> 1998  10  0  392.
> 1998  11  0  346.
> 1998  12  0  387.
> 1998  13  0  393.
> 1998  14  0  367.
> 1998  15  0  320.
> 1998  16  0  309.
> 1998  17  0  341.
> 1998  18  0  329.
> 1998  19  0  322.
> 1998  20  0  429.
> 1998  21  0  433.
> 1998  22  0  398.
> 1998  23  0  393.
> 1998  24  0  393.
> 1998  25  0  423.
> 1998  26  0  426.
> 1998  27  0  429.
> 1998  28  0  386.
> 1998  29  0  381.
> 1998  30  0  375.
> 1998  31  0  365.
> 1998  32  0  450.
> 1998  33  0  381.
> 1998  34  0  316.
> 1998  35  0  351.
> 1998  36  0  306.
> 1998  37  0  312.
> 1998  38  0  320.
> 1998  39  0  339.
> 1998  40  0  395.
> 1998  41  0  429.
> 1998  42  0  479.
> 1998  43  0  495.
> 1998  44  0  407.
> 1998  45  0  358.
> 1998  46  0  360.
> 1998  47  0  382.
> 1998  48  0  394.
> 1998  49  0  393.
> 1998  50  0  435.
> 1998  51  0  408.
> 1998  52  0  360.
> 1998  53  0  372.
> 1998  54  0  376.
> 1998  55  0  379.
> 1998  56  0  361.
> 1998  57  0  333.
> 1998  58  0  321.
> 1998  59  0  344.
> 1998  60  0  412.
> 1998  61  0  428.
> 1998  62  0  401.
> 1998  63  0  369.
> 1998  64  0  343.
> 1998  65  0  330.
> 1998  66  0  317.
> 1998  67  0  296.
> 1998  68  0  282.
> 1998  69  0  404.
> 1998  70  0  530.
> 1998  71  0  525.
> 1998  72  0  484.
> 1998  73  0  430.
> 1998  74  0  388.
> 1998  75  0  347.
> 1998  76  0  337.
> 1998  77  0  342.
> 1998  78  0  305.
> 1998  79  0  329.
> 1998  80  0  420.
> 1998  81  0  564.
> 1998  82  0  483.
> 1998  83  0  385.
> 1998  84  0  393.
> 1998  85  0  437.
> 1998  86  0  441.
> 1998  87  0  434.
> 1998  88  0  471.
> 1998  89  0  429.
> 1998  90  0  412.
> 1998  91  0  370.
> 1998  92  0  326.
> 1998  93  0  357.
> 1998  94  0  338.
> 1998  95  0  380.
> 1998  96  0  339.
> 1998  97  0  312.
> 1998  98  0  313.
> 1998  99  0  327.
> 1998 100  0  362.
> 1998 101  0  358.
> 1998 102  0  387.
> 1998 103  0  397.
> 1998 104  0  375.
> 1998 105  0  350.
> 1998 106  0  357.
> 1998 107  0  472.
> 1998 108  0  526.
> 1998 109  0  396.
> 1998 110  0  374.
> 1998 111  0  376.
> 1998 112  0  355.
> 1998 113  0  343.
> 1998 114  0  425.
> 1998 115  0  426.
> 1998 116  0  479.
> 1998 117  0  469.
> 1998 118  0  425.
> 1998 119  0  344.
> 1998 120  0  341.
> 1998 121  0  426.
> 1998 122  0  601.
> 1998 123  0  476.
> 1998 124  0  670.
> 1998 125  0  585.
> 1998 126  0  496.
> 1998 127  0  479.
> 1998 128  0  569.
> 1998 129  0  531.
> 1998 130  0  489.
> 1998 131  0  484.
> 1998 132  0  480.
> 1998 133  0  393.
> 1998 134  0  332.
> 1998 135  0  327.
> 1998 136  0  493.
> 1998 137  0  493.
> 1998 138  0  430.
> 1998 139  0  396.
> 1998 140  0  408.
> 1998 141  0  416.
> 1998 142  0  376.
> 1998 143  0  375.
> 1998 144  0  415.
> 1998 145  0  407.
> 1998 146  0  398.
> 1998 147  0  352.
> 1998 148  0  349.
> 1998 149  0  517.
> 1998 150  0  597.
> 1998 151  0  480.
> 1998 152  0  435.
> 1998 153  0  408.
> 1998 154  0  441.
> 1998 155  0  397.
> 1998 156  0  374.
> 1998 157  0  413.
> 1998 158  0  582.
> 1998 159  0  513.
> 1998 160  0  459.
> 1998 161  0  466.
> 1998 162  0  414.
> 1998 163  0  354.
> 1998 164  0  341.
> 1998 165  0  343.
> 1998 166  0  369.
> 1998 167  0  411.
> 1998 168  0  355.
> Thanks
> Jibrin
> 
> On Fri, Jan 15, 2021 at 7:15 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:
> 
>> Hello,
>> 
>> No dataset was attached. Like the posting guide says,
>> 
>> No binary attachments except for PS, PDF, and some image and archive
>> formats (others are automatically stripped off because they can contain
>> malicious software). Files in other formats and larger ones should
>> rather be put on the web and have only their URLs posted. This way a
>> reader has the option to download them or not.
>> 
>> 
>> Can you post sample data? Please post the output of `dput(df)`. Or, if
>> it is too big the output of `dput(head(df, 20))`. (`df` is the name of
>> your dataset.)
>> 
>> Hope this helps,
>> 
>> Rui Barradas
>> 
>> Às 11:30 de 15/01/21, Jibrin Alhassan escreveu:
>>> Dear R users,
>>> I am very new to R software. I have solar wind speed data needed for my
>>> work. How do I convert day in the year to year, month, and day with R
>>> software? I have used this code
>>> as.Date(0, origin = "1998-01-01")
>>> but it can only convert one day of the year at a time. Meanwhile, I have
>> up
>>> to the 1998-2002 data set. Attached is my data.
>>> Kindly help, please.
>>> Jibrin Alhassan
>>> ______________________________________________
>>> R-help using 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]]
> 
> ______________________________________________
> R-help using 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes using cbs.dk  Priv: PDalgd using gmail.com



More information about the R-help mailing list