0

I try to change a date variable from factor format to date format but I can't and I don't know why.

Here is my code

temp_tot <-read.csv("temp_total_030820.csv",header=TRUE,sep=",",na.strings=".", stringsAsFactors=FALSE)
class(temp_tot$date)#character
temp_tot$date<-as.Date(temp_tot$date,format="%m/%d/%y")#returning N.A

Here is an output of my data

structure(list(X = 1L, capteur = "30-01-s.", Year = 2021L, jj = 1L, 
               variable = "tmax", value = 0.517166666666667, dpt = "Aigoual", 
               pos = "sol", date = "2021-01-01"), row.names = 1L, class = "data.frame")

I've tried a lot of functions but I don't know where I'm stucked. Does it has something to do with the "Locale" ?

Thank you !

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

1

Deep down in the Value section of strptime we find If the specified time is invalid (for example ‘"2010-02-30 08:00"’) all the components of the result are ‘NA’. That date looks similar to yours and we'll guess a valid date (as input) would go %m%d%Y or %d%m%Y:

splits <- strsplit(temp_tot$date, '-')[[1]]
reversed <- rev(splits)
my_valid_date <- as.Date(paste(reversed[1], reversed[2], reversed[3], sep='/'))
class(my_valid_date)
[1] "Date"

I don't know if you are doing European or American date format so I leave that to you. Starting with 01-01 is ambiguous, and might impact the order you put your reversed parts above. HTH

Chris
  • 1,647
  • 1
  • 18
  • 25
  • Thank you for your answer. The right format for my case would be "%d%m%Y". But I dont really get how the initial format can't be recognized as date. On the original script I used I could create a date with this line : "m_j_table$date=as.POSIXct(strptime(paste(m_j_table$Year,m_j_table$jj,sep="_"),"%Y_%j"))". How is that possible then ? Thanks again for your help. I'm trying with your code but the "as.date" isn't working, here is the error : "Error in charToDate(x) : character string is not in a standard unambiguous format". What did I do wrong ? – Germain Vital Aug 03 '21 at 20:28
  • I can only suggest if you tell me what you typed this time, not before. For an overview of the wonderful confusions over eight years [standard unambiguous date](https://stackoverflow.com/questions/14755425/what-are-the-standard-unambiguous-date-formats-for-string-to-date-conversion-i) and 211k views. – Chris Aug 03 '21 at 20:39
  • Of course sorry here is what I typed : "splits <- strsplit(temp_tot$date, '-') reversed <- rev(splits) my_valid_date <- as.Date(paste(reversed[3], reversed[2], reversed[1], sep='/',format = "%d/%m/%Y"))" Thank you for the link I already took a quick look at it tho – Germain Vital Aug 03 '21 at 20:56
  • Yes, sorry, my cut and paste error from terminal, the `)[[1]]` on the strsplit takes out the list return and allows the following paste to work properly. See edit. – Chris Aug 03 '21 at 21:09
  • I just tried with as.date.character and it work my variable is now in date format. So %Y %m %d format can work with as.date but not with strptime ? – Germain Vital Aug 03 '21 at 21:18
  • I wouldn't venture a guess as as.Date.character is alias for as.Date (in help) which is where the question began and my attempt here was to just solve the problem of the moment, looking with fear and trepidation at the [standard unambiguous](https://stackoverflow.com/questions/14755425/what-are-the-standard-unambiguous-date-formats-for-string-to-date-conversion-i), I just fish around for something that works, as you just did. Perhaps i'm a chicken. But I'll stick with this answer. – Chris Aug 03 '21 at 21:28
  • No worries and thanks again for your help! – Germain Vital Aug 03 '21 at 21:42
  • Are you accepting this as a possible answer? – Chris Aug 03 '21 at 21:50