[R] Converting "day of year" to "year", "month" and "day"
Martin Møller Skarbiniks Pedersen
tr@xp|@yer @end|ng |rom gm@||@com
Fri Jan 15 23:25:32 CET 2021
On Fri, 15 Jan 2021 at 18:55, Jibrin Alhassan <jibrin.alhassan using unn.edu.ng>
wrote:
>
> 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")
Look at the package lubridate.
Here is an example for you:
library(lubridate)
v <- seq(ymd("2020-01-01"),ymd("2022-01-01"),1)
df <- data.frame(date = v, day = day(v), month = month(v),year = year(v))
str(df)
head(df,3)
tail(df,3)
'data.frame': 732 obs. of 4 variables:
$ date : Date, format: "2020-01-01" "2020-01-02" ...
$ day : int 1 2 3 4 5 6 7 8 9 10 ...
$ month: num 1 1 1 1 1 1 1 1 1 1 ...
$ year : num 2020 2020 2020 2020 2020 2020 2020 2020 2020 2020 ...
date day month year
1 2020-01-01 1 1 2020
2 2020-01-02 2 1 2020
3 2020-01-03 3 1 2020
date day month year
730 2021-12-30 30 12 2021
731 2021-12-31 31 12 2021
732 2022-01-01 1 1 2022
Regards
Martin
[[alternative HTML version deleted]]
More information about the R-help
mailing list