Jibrin Alhassan
2021-Jan-15 11:30 UTC
[R] Converting "day of year" to "year", "month" and "day"
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
Rui Barradas
2021-Jan-15 18:15 UTC
[R] Converting "day of year" to "year", "month" and "day"
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 at 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. >
Bert Gunter
2021-Jan-15 18:49 UTC
[R] Converting "day of year" to "year", "month" and "day"
There are many good tutorials for R. As a "newbie", you need to avail yourself of them. Although this forum is meant to "help", it is not designed to provide tutorials. Understanding basic R functionality is largely assumed here. Searching on "tutorials on date-time data in R" brought up many possibilities. Choose one or more that best suits your needs. As for your specific query, you seem not to understand R's "vectorization" behavior: your statement, "it can only convert one day of the year at a time", is false. Again, search for a tutorial on "vectorization in R." But note that the "Intro to R" tutorial that ships with R already has this. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Fri, Jan 15, 2021 at 9:55 AM Jibrin Alhassan <jibrin.alhassan at 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") > 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 at 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]]
Bill Dunlap
2021-Jan-15 19:20 UTC
[R] Converting "day of year" to "year", "month" and "day"
Use one of the POSIXt classes, POSIXct or POSIXlt, instead of the Date class. They have more methods for doing arithmetic. E.g.,> dates <- as.POSIXct(tz="UTC", c("2004-03-01", "2005-03-01")) > difftime(dates, trunc(dates, units="year"), units="days") # add 1 if youwant YYYY-01-01 to be day 1 instead of day 0 Time differences in days [1] 60 59 There are packages like 'zoo' that make many time/date operations simpler, but the above works with core R. -Bill On Fri, Jan 15, 2021 at 9:55 AM Jibrin Alhassan <jibrin.alhassan at 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") > 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 at 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]]
Martin Møller Skarbiniks Pedersen
2021-Jan-15 22:25 UTC
[R] Converting "day of year" to "year", "month" and "day"
On Fri, 15 Jan 2021 at 18:55, Jibrin Alhassan <jibrin.alhassan at 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]]
Hi Jibrin, solar_wind_sps<-data.frame(sws=306,date="2021-016") solar_wind_spd solar_wind_spd$date<-as.Date(solar_wind_spd$date,"%Y-%j") solar_wind_spd This changes the "date" field to an actual date object. If you just want to change a character string date to another format: solar_wind_spd$date<-format(as.Date(solar_wind_spd$date,%Y-%j),"%Y-%m-%d") Jim On Sat, Jan 16, 2021 at 4:55 AM Jibrin Alhassan <jibrin.alhassan at 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") > 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 at 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.
Jibrin Alhassan
2021-Jan-21 10:42 UTC
[R] Problem on Converting "day of year" to "year", "month" and "day" has been solved
Dear R users,> I want to thank you all for your contributions to the problem I posted. It > has been solved. Find below the code that solved the problem. >df1 <- read.table("SWS1998_2002", header = TRUE) df1$date <- as.Date(paste(df1$year, df1$day), format = "%Y %j", origin = "1998-01-01") df2 <- df1[c("date", "Dst")] head(df2) #To display all the rows print(df2 Thanks, Jibrin Alhassan [[alternative HTML version deleted]]