I have solar-geophysical data e.g as blow: YEAR DOY HR IMF SW SSN Dst f10.7 2012 214 0 3.4 403. 132 -9 154.6 2012 214 1 3.7 388. 132 -10 154.6 2012 214 2 3.7 383. 132 -10 154.6 2012 214 3 3.7 391. 132 -9 154.6 2012 214 4 4.2 399. 132 -7 154.6 2012 214 5 4.1 411. 132 -6 154.6 2012 214 6 4.0 407. 132 -6 154.6 2012 214 7 4.2 404. 132 -4 154.6 2012 214 8 4.3 405. 132 -6 154.6 2012 214 9 4.4 409. 132 -6 154.6 2012 214 10 4.4 401. 132 -6 154.6 2012 214 11 4.5 385. 132 -7 154.6 2012 214 12 4.7 377. 132 -8 154.6 2012 214 13 4.7 382. 132 -6 154.6 2012 214 14 4.3 396. 132 -4 154.6 2012 214 15 4.1 384. 132 -2 154.6 2012 214 16 4.0 382. 132 -1 154.6 2012 214 17 3.9 397. 132 0 154.6 2012 214 18 3.8 390. 132 1 154.6 2012 214 19 4.2 400. 132 2 154.6 2012 214 20 4.6 408. 132 1 154.6 2012 214 21 4.8 401. 132 -3 154.6 2012 214 22 4.9 395. 132 -5 154.6 2012 214 23 5.0 386. 132 -1 154.6 2012 215 0 5.0 377. 143 -1 138.6 2012 215 1 4.9 384. 143 -2 138.6 2012 215 2 4.9 390. 143 -4 138.6 2012 215 3 4.9 372. 143 -6 138.6 2012 215 4 5.1 371. 143 -4 138.6 I want to process it to be of the format as shown below y m d hr imf sws ssn Dst f10.7 2012-08-01 10 3.4 403. 132 -9 154.6 2012-08-01 12 3.7 388. 132 -10 154.6 2012-08-01 15 3.7 383. 132 -10 154.6 2012-08-01 17 3.7 391. 132 -9 154.6 I want to request an R code to accomplish this task. Thanks for your time. *Jibrin Adejoh Alhassan (Ph.D)* Department of Physics and Astronomy, University of Nigeria, Nsukka [[alternative HTML version deleted]]
?s 20:00 de 15/06/2024, Jibrin Alhassan escreveu:> I have solar-geophysical data e.g as blow: > YEAR DOY HR IMF SW SSN Dst f10.7 > 2012 214 0 3.4 403. 132 -9 154.6 > 2012 214 1 3.7 388. 132 -10 154.6 > 2012 214 2 3.7 383. 132 -10 154.6 > 2012 214 3 3.7 391. 132 -9 154.6 > 2012 214 4 4.2 399. 132 -7 154.6 > 2012 214 5 4.1 411. 132 -6 154.6 > 2012 214 6 4.0 407. 132 -6 154.6 > 2012 214 7 4.2 404. 132 -4 154.6 > 2012 214 8 4.3 405. 132 -6 154.6 > 2012 214 9 4.4 409. 132 -6 154.6 > 2012 214 10 4.4 401. 132 -6 154.6 > 2012 214 11 4.5 385. 132 -7 154.6 > 2012 214 12 4.7 377. 132 -8 154.6 > 2012 214 13 4.7 382. 132 -6 154.6 > 2012 214 14 4.3 396. 132 -4 154.6 > 2012 214 15 4.1 384. 132 -2 154.6 > 2012 214 16 4.0 382. 132 -1 154.6 > 2012 214 17 3.9 397. 132 0 154.6 > 2012 214 18 3.8 390. 132 1 154.6 > 2012 214 19 4.2 400. 132 2 154.6 > 2012 214 20 4.6 408. 132 1 154.6 > 2012 214 21 4.8 401. 132 -3 154.6 > 2012 214 22 4.9 395. 132 -5 154.6 > 2012 214 23 5.0 386. 132 -1 154.6 > 2012 215 0 5.0 377. 143 -1 138.6 > 2012 215 1 4.9 384. 143 -2 138.6 > 2012 215 2 4.9 390. 143 -4 138.6 > 2012 215 3 4.9 372. 143 -6 138.6 > 2012 215 4 5.1 371. 143 -4 138.6 > I want to process it to be of the format as shown below > y m d hr imf sws ssn Dst f10.7 > 2012-08-01 10 3.4 403. 132 -9 154.6 > 2012-08-01 12 3.7 388. 132 -10 154.6 > 2012-08-01 15 3.7 383. 132 -10 154.6 > 2012-08-01 17 3.7 391. 132 -9 154.6 > I want to request an R code to accomplish this task. Thanks for your time. > *Jibrin Adejoh Alhassan (Ph.D)* > Department of Physics and Astronomy, > University of Nigeria, Nsukka > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.Hello, To create a date column, paste the first two columns and coerce to class "Date" with conversion specifications %Y for the 4 digit year and %j for the day of year. See help("strptime") df1 <- read.table(text = "YEAR DOY HR IMF SW SSN Dst f10.7 2012 214 0 3.4 403. 132 -9 154.6 2012 214 1 3.7 388. 132 -10 154.6 2012 214 2 3.7 383. 132 -10 154.6 2012 214 3 3.7 391. 132 -9 154.6 2012 214 4 4.2 399. 132 -7 154.6 2012 214 5 4.1 411. 132 -6 154.6 2012 214 6 4.0 407. 132 -6 154.6 2012 214 7 4.2 404. 132 -4 154.6 2012 214 8 4.3 405. 132 -6 154.6 2012 214 9 4.4 409. 132 -6 154.6 2012 214 10 4.4 401. 132 -6 154.6 2012 214 11 4.5 385. 132 -7 154.6 2012 214 12 4.7 377. 132 -8 154.6 2012 214 13 4.7 382. 132 -6 154.6 2012 214 14 4.3 396. 132 -4 154.6 2012 214 15 4.1 384. 132 -2 154.6 2012 214 16 4.0 382. 132 -1 154.6 2012 214 17 3.9 397. 132 0 154.6 2012 214 18 3.8 390. 132 1 154.6 2012 214 19 4.2 400. 132 2 154.6 2012 214 20 4.6 408. 132 1 154.6 2012 214 21 4.8 401. 132 -3 154.6 2012 214 22 4.9 395. 132 -5 154.6 2012 214 23 5.0 386. 132 -1 154.6 2012 215 0 5.0 377. 143 -1 138.6 2012 215 1 4.9 384. 143 -2 138.6 2012 215 2 4.9 390. 143 -4 138.6 2012 215 3 4.9 372. 143 -6 138.6 2012 215 4 5.1 371. 143 -4 138.6", header = TRUE) with(df1, paste(YEAR, DOY)) |> as.Date(format = "%Y %j") #> [1] "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" #> [6] "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" #> [11] "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" #> [16] "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" #> [21] "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-01" "2012-08-02" #> [26] "2012-08-02" "2012-08-02" "2012-08-02" "2012-08-02" # now create the column df1$Date <- with(df1, paste(YEAR, DOY)) |> as.Date(format = "%Y %j") # remove the columns no longer needed df1 <- df1[-(1:2)] # relocate the new date column df1 <- df1[c(ncol(df1), 1:(ncol(df1) - 1L))] head(df1) #> Date HR IMF SW SSN Dst f10.7 #> 1 2012-08-01 0 3.4 403 132 -9 154.6 #> 2 2012-08-01 1 3.7 388 132 -10 154.6 #> 3 2012-08-01 2 3.7 383 132 -10 154.6 #> 4 2012-08-01 3 3.7 391 132 -9 154.6 #> 5 2012-08-01 4 4.2 399 132 -7 154.6 #> 6 2012-08-01 5 4.1 411 132 -6 154.6 Hope this helps, Rui Barradas -- Este e-mail foi analisado pelo software antiv?rus AVG para verificar a presen?a de v?rus. www.avg.com
library(lubridate) library(dplyr) df1 <- read.table(text = "YEAR DOY HR IMF SW SSN Dst f10.7 2012 214 0 3.4 403. 132 -9 154.6 2012 214 1 3.7 388. 132 -10 154.6 2012 214 2 3.7 383. 132 -10 154.6 2012 214 3 3.7 391. 132 -9 154.6 2012 215 4 5.1 371. 143 -4 138.6", header = TRUE) df_date <- df1 |> mutate( date_date = as.Date(DOY-1, origin=paste0(YEAR, "-01-01")), date_chr = format(date_date, "%Y-%m-%d") ) df_date <- df_date[,-c(1:2,9)] df_date <- df_date[c(ncol(df_date), 1:(ncol(df_date) - 1L))] # That is the form of the data in the example. # But there may be a conflict in the column names versus the data shown where Y, M, and D were separate versus "Y_M_D" which would be the name of one column indicating the desired order. # If you want Y, M, and D in different columns then this could work: df_date2 <- df1 |> mutate( date_date = as.Date(DOY-1, origin=paste0(YEAR, "-01-01")), date_chr = format(date_date, "%Y-%m-%d") ) df_date2$M <- month(df_date2$date_date) df_date2$D <- day(df_date2$date_date) # drop unused columns df_date2 <- df_date2[ , -c(2, 9, 10)] # Reorganize columns to desired locations df_date2 <- df_date2[, c("YEAR", "M", "D", "IMF", "SW", "SSN", "Dst", "f10.7")] # Rename "Year" to "Y" names(df_date2)[names(df_date2) == "YEAR"] <- "Y" Tim -----Original Message----- From: R-help <r-help-bounces at r-project.org> On Behalf Of Jibrin Alhassan Sent: Saturday, June 15, 2024 3:01 PM To: R-help <R-help at r-project.org> Subject: [R] code for year month day hr format [External Email] I have solar-geophysical data e.g as blow: YEAR DOY HR IMF SW SSN Dst f10.7 2012 214 0 3.4 403. 132 -9 154.6 2012 214 1 3.7 388. 132 -10 154.6 2012 214 2 3.7 383. 132 -10 154.6 2012 214 3 3.7 391. 132 -9 154.6 2012 214 4 4.2 399. 132 -7 154.6 2012 214 5 4.1 411. 132 -6 154.6 2012 214 6 4.0 407. 132 -6 154.6 2012 214 7 4.2 404. 132 -4 154.6 2012 214 8 4.3 405. 132 -6 154.6 2012 214 9 4.4 409. 132 -6 154.6 2012 214 10 4.4 401. 132 -6 154.6 2012 214 11 4.5 385. 132 -7 154.6 2012 214 12 4.7 377. 132 -8 154.6 2012 214 13 4.7 382. 132 -6 154.6 2012 214 14 4.3 396. 132 -4 154.6 2012 214 15 4.1 384. 132 -2 154.6 2012 214 16 4.0 382. 132 -1 154.6 2012 214 17 3.9 397. 132 0 154.6 2012 214 18 3.8 390. 132 1 154.6 2012 214 19 4.2 400. 132 2 154.6 2012 214 20 4.6 408. 132 1 154.6 2012 214 21 4.8 401. 132 -3 154.6 2012 214 22 4.9 395. 132 -5 154.6 2012 214 23 5.0 386. 132 -1 154.6 2012 215 0 5.0 377. 143 -1 138.6 2012 215 1 4.9 384. 143 -2 138.6 2012 215 2 4.9 390. 143 -4 138.6 2012 215 3 4.9 372. 143 -6 138.6 2012 215 4 5.1 371. 143 -4 138.6 I want to process it to be of the format as shown below y m d hr imf sws ssn Dst f10.7 2012-08-01 10 3.4 403. 132 -9 154.6 2012-08-01 12 3.7 388. 132 -10 154.6 2012-08-01 15 3.7 383. 132 -10 154.6 2012-08-01 17 3.7 391. 132 -9 154.6 I want to request an R code to accomplish this task. Thanks for your time. *Jibrin Adejoh Alhassan (Ph.D)* Department of Physics and Astronomy, University of Nigeria, Nsukka [[alternative HTML version deleted]] ______________________________________________ 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.