0
votes

I had problem in specifying the date format of my data arranged as:

Date    Longitude   Latitude    Elevation   Max Temperature Min Temperature Precipitation   Wind    Relative Humidity   Solar
1/1/1979    40.625  10.1473999  594 31.784  14.711  0   1.34012963  0.332977248 23.69273112
1/2/1979    40.625  10.1473999  594 30.548  15.001  0   1.440527678 0.445704584 23.2192062
1/3/1979    40.625  10.1473999  594 30.029  18.724  0.020599366 1.950813349 0.532062642 18.34447248
1/4/1979    40.625  10.1473999  594 31.311  17.802  0   2.150911946 0.466495869 23.07077748

i tried using as.Date but the result is as follows:

gewane_t$Date <- as.Date(gewane_t$Date,format("%m/%d/%Y"))

Error in charToDate(x) : character string is not in a standard unambiguous format

any solution.

kind regards,

1
Instead of as.Date(gewane_t$Date, format("%m/%d/%Y") try as.Date(gewane_t$Date, format = "%m/%d/%Y").ricoderks
This is the outcome. Any suggestions.gewane_t$Date <- as.Date(gewane_t$Date, format = "%m/%d/%Y") Error in as.Date.numeric(gewane_t$Date, format = "%m/%d/%Y") : 'origin' must be suppliedYon Balcha
That's a strange message. This would suggest that gewane_t$Date is numerical. From the table above and your error message I would suggest it is a character string. Was this file open in Excel and saved? Excel shows a date, but in the background it is just a number.ricoderks
yes it was saved in excel.Yon Balcha
Ah ok, if the column was in date format and you save it as a csv file then it will be saved as a number (i.e. number of days as of 01-01-1900 on Windows machine and 01-01-1904 on Mac machine). You can try: as.Date(gewane_t$Date, origin = "1900-01-01")ricoderks

1 Answers

1
votes

Use lubridate dmy function. This function automatically returns objects of class POSIXct and will work with either factors or characters.

Assuming your dataframe is called gewane_t update of code dmy to mdy

library(lubridate)
gewane_t$Date <- mdy(gewane_t$Date)

Output:

  Date       Longitude Latitude Elevation   Max Temperature    Min Temperature_1 Precipitation  Wind
  <date>         <dbl>    <dbl>     <dbl> <dbl>       <dbl>  <dbl>         <dbl>         <dbl> <dbl>
1 1979-01-01      40.6     10.1       594  31.8        14.7 0               1.34         0.333  23.7
2 1979-02-01      40.6     10.1       594  30.5        15.0 0               1.44         0.446  23.2
3 1979-03-01      40.6     10.1       594  30.0        18.7 0.0206          1.95         0.532  18.3
4 1979-04-01      40.6     10.1       594  31.3        17.8 0               2.15         0.466  23.1