[R] code for year month day hr format
Rui Barradas
ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun Jun 16 09:33:03 CEST 2024
Às 21:42 de 15/06/2024, Jibrin Alhassan escreveu:
> Thank you Rui. I ran the following script
> df1 <- read.table("solar_hour", header = TRUE)
> df1$date <- as.Date(paste(df1$year, df1$hour),
> format = "%Y %j",
> origin = "2012-08-01-0")
> df2 <- df1[c("date", "IMF", "SWS", "SSN", "Dst", "f10")]
> head(df1)
> #To display all the rows
> print(df2).
> It gave me this error message
>> source ("script.R")
> Error in `$<-.data.frame`(`*tmp*`, date, value = numeric(0)) :
> replacement has 0 rows, data has 38735
>> print(df2)
> Error: object 'df2' not found
>> My data is an hourly data but desire to have the date as
> year month day hour
> 2012 08 01 01
> 2012 08 01 02
> 2012 08 01 03 etc
> Thanks.
>
> *Jibrin Adejoh Alhassan (Ph.D)*
> Department of Physics and Astronomy,
> University of Nigeria, Nsukka
>
>
> On Sat, Jun 15, 2024 at 8:34 PM Rui Barradas <ruipbarradas using sapo.pt> wrote:
>
>> À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 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.
>> 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
>>
>
Hello,
There is an error in your new code:
paste YEAR with DOY, not with HR.
As for the rest, is your real data like the one you posted before?
If it is then I don't see anything wrong with my (tested) solution.
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
More information about the R-help
mailing list