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

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Jan 18 00:13:54 CET 2021


Hello,

My code didn't produce only 6 rows, it displayed only 6 rows. But all 
rows now have a date column.

As for the first question,

df2 <- df1[c("SWS", "date")]


selects the columns with those names. I didn't rewrite the original df1, 
but if you want to, assign to df1, without creating df2.

Hope this helps,

Rui Barradas

Às 15:12 de 17/01/21, Jibrin Alhassan escreveu:
> Hi Barradas,
> Thanks for your assistance. It has brought me closer to what I am 
> looking for. I tried your code as shown below:
>  > df1 <- read.table("SWSdata_1998_2002", header = TRUE)
>  > 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
> What I need is the last two columns only (SWS, date). The first 3 
> columns (year, day Hr should go). Your code produced only 6 datasets. My 
> dataset is 1,826 from 1998 to 2002. How do I generate this at once?
> Many many thanks for your time. I have pasted below a section of my 
> dataset for your guidance, please.
> Jibrin
> 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.
> 1998 169  0  333.
> 1998 170  0  443.
> 1998 171  0  426.
> 1998 172  0  419.
> 1998 173  0  404.
> 1998 174  0  387.
> 1998 175  0  460.
> 1998 176  0  447.
> 1998 177  0  469.
> 1998 178  0  447.
> 1998 179  0  389.
> 1998 180  0  375.
> 1998 181  0  354.
> 1998 182  0  316.
> 1998 183  0  369.
> 1998 184  0  410.
> 1998 185  0  406.
> 1998 186  0  477.
> 1998 187  0  583.
> 1998 188  0  458.
> 1998 189  0  386.
> 1998 190  0  342.
> 1998 191  0  333.
> 1998 192  0  369.
> 1998 193  0  406.
> 1998 194  0  375.
> 1998 195  0  332.
> 1998 196  0  310.
> 1998 197  0  528.
> 1998 198  0  530.
> 1998 199  0  387.
> 1998 200  0  385.
> 1998 201  0  349.
> 1998 202  0  409.
> 1998 203  0  399.
> 1998 204  0  619.
> 1998 205  0  658.
> 1998 206  0  581.
> 1998 207  0  445.
> 1998 208  0  370.
> 1998 209  0  326.
> 1998 210  0  334.
> 1998 211  0  384.
> 1998 212  0  423.
> 1998 213  0  412.
> 1998 214  0  404.
> 1998 215  0  370.
> 1998 216  0  384.
> 1998 217  0  383.
> 1998 218  0  378.
> 1998 219  0  461.
> 1998 220  0  460.
> 1998 221  0  400.
> 1998 222  0  447.
> 1998 223  0  373.
> 1998 224  0  379.
> 1998 225  0  374.
> 1998 226  0  374.
> 1998 227  0  391.
> 1998 228  0  348.
> 1998 229  0  303.
> 1998 230  0  279.
> 1998 231  0  312.
> 1998 232  0  331.
> 1998 233  0  298.
> 1998 234  0  341.
> 1998 235  0  493.
> 1998 236  0  436.
> 1998 237  0  400.
> 1998 238  0  633.
> 1998 239  0  630.
> 1998 240  0  583.
> 1998 241  0  547.
> 1998 242  0  550.
> 1998 243  0  499.
> 1998 244  0  444.
> 1998 245  0  427.
> 1998 246  0  401.
> 
> On Sat, Jan 16, 2021 at 8:01 AM Rui Barradas <ruipbarradas using sapo.pt 
> <mailto:ruipbarradas using sapo.pt>> wrote:
> 
>     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>
>      > <mailto: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>
>     <mailto: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