0

I am trying to work with the "airGRTeaching" package in R. it aims to fit a simulated flow "Qs" to an observed one "Q" using some mathematical equations and input variables (P and E).

I have prepared my input file that contains among other thing a column named "jour" that refers to date. here is how the table file named Book1.CSV looks like

        jour    P     E     Q
1    1-1-1991 13.0 2.464 0.072
2    1-2-1991 15.6 1.799 0.068
3    1-3-1991 13.6 1.379 0.150
4    1-4-1991  0.0 1.628 0.605
5    1-5-1991  7.8 2.000 0.535
6    1-6-1991 15.2 1.606 0.367
7    1-7-1991  1.3 1.812 1.153
8    1-8-1991  0.0 2.222 1.768
9    1-9-1991  0.3 2.700 0.999
10  1-10-1991  0.0 2.354 0.695

As demanded, the data file should be in date.frame format so I converted using the line below:

> mydata<-read.csv("C:\\Users\\AcER PREDATOR\\Desktop\\Book1.CSV", header=TRUE)
> df<-data.frame(mydata)

Now I want to convert the date column (jour) into "POSIXt" format UTC time zone. So, I tried:

> DATE<- as.POSIXlt(df[1], tz="UTC", format= c("%Y-%m-%D %H:%M/%OS"), optional = FALSE)

but I get this error message:

Error in as.POSIXlt.default(df[1], tz = "UTC", format = c("%Y-%m-%D %H:%M/%OS"),  : 
  do not know how to convert 'df[1]' to class “POSIXlt”

Any suggestions how to resolve this issue is appreciable. Thank's

Momkin
  • 1
  • 2
    Looking at your `jour`, it looks nothing like `"%Y-%m-%D %H:%M/%OS"`. I would expect something like `"%m-%d-%Y"` (no time component). – r2evans May 01 '20 at 00:03
  • 2
    ... but that error looks like your first column is a `factor` instead of `character`. Consider `as.POSIXlt(as.character(df[1]), format="%m-%d-%Y")`. – r2evans May 01 '20 at 00:05

1 Answers1

0

Using parse_date_time function from lubridate you can obtain:

library(lubridate)

Date <- parse_date_time(df$jour, orders = "%m-%d-%Y", tz = "UTC")

 [1] "1991-01-01 UTC" "1991-01-02 UTC" "1991-01-03 UTC" "1991-01-04 UTC" "1991-01-05 UTC" "1991-01-06 UTC" "1991-01-07 UTC"
 [8] "1991-01-08 UTC" "1991-01-09 UTC" "1991-01-10 UTC"

Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32
  • yes it worked, even though I needed to downgrade to R 3.6.3 to support the "lubridate" package. Thank you for your help dc37. – Momkin May 02 '20 at 14:06