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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sat Jan 16 08:01:16 CET 2021


Hello,

Thanks for the data, it makes things easier.

df1 <- read.table("Jibrin_data.txt", header = TRUE)
#'data.frame':	168 obs. of  4 variables:
# $ year: int  1998 1998 1998 1998 1998 1998 1998 1998 1998 1998 ...
# $ day : int  1 2 3 4 5 6 7 8 9 10 ...
# $ Hr  : int  0 0 0 0 0 0 0 0 0 0 ...
# $ SWS : num  344 346 356 332 302 329 395 359 471 392 ...

Here is a simple way of converting the year and day of year columns to a 
column of class "Date".
Like others have said, there are also CRAN packages to handle date/time 
data, my favorite being package lubridate, but base R can do it.


df1$date <- as.Date(paste(df1$year, df1$day),
                     format = "%Y %j",
                     origin = "1998-01-01")

head(df1)
#  year day Hr SWS       date
#1 1998   1  0 344 1998-01-01
#2 1998   2  0 346 1998-01-02
#3 1998   3  0 356 1998-01-03
#4 1998   4  0 332 1998-01-04
#5 1998   5  0 302 1998-01-05
#6 1998   6  0 329 1998-01-06


Hope this helps,

Rui Barradas


Às 06:48 de 16/01/21, Jibrin Alhassan escreveu:
> 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 
> <mailto: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 <mailto: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.
>      >
>



More information about the R-help mailing list