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-boun...@r-project.org> On Behalf Of Jibrin Alhassan
Sent: Saturday, June 15, 2024 3:01 PM
To: R-help <R-help@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@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.

______________________________________________
R-help@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.

Reply via email to