r - Converting character to Date -
I have a vector with dates in the following format:
dates & lt; - c ("01AUG2006", "01DEC2006", "01JC2006", "01JUN2006", "01MAY2007") To convert this vector to something in a class, Date , I wrote this function: convert2Date & lt; - Function (x) {Required (car) D & lt; - substr (x, 0,2) m.text & lt; - substr (x, 3,5) m & lt; - Record (Mtext, "'Jane' = 1; 'FB' = 2; 'MAR' = 3; 'APR' = 4; 'May' = 5; 'Jun' = 6; 'Jul' = 7; 'AuG' = 8; 'SEP' = 9; 'OCT' = 10; 'Novi' = 11; 'DEC' = 12 ") Y & LT; - substrate (x, 69) out & lt; - Date (code (D, M, Y, Sep = "/"), "% d /% m /% Y" outside}
test & lt; - Convert 2 dated (date) [1] "2006-08-01" "2006-12-01" "2006-06-01" "2007 -05- 01 "class (test) [1]" date " This works, but it is somewhat complicated and only works for this specific format. There is a common problem, so there should be a simpler and more versatile way of doing this. Any suggestions? Thanks a lot!
Without local resets, <00> lubridate :: dmy will work for me if I change months into Portuguese abrev form.
DM (C ("01AGO2006", "01DEZ2006", "01JUN2006", "01MAI2007")) [1] "2006-08-01 UTC" "2006-12-01 UTC" Set the locale from the 2006-06-01 UTC "2007 -05-01 UTC" or the DMI Function call, which makes it very easy.
< Code> dmy (date, location = "English_United states.1252") # without resizing locale [1] "2006-08-01 UTC" "2006-12-01 UTC" 2006-06-01 UTC "" 2007-05- 01 UTC " It's OK right now.
For non-English systems, locale should be changed accordingly or the text should be re-written.
Reset the locale to US English 2252 ("01AUG2006", "01DEC2006", "01JUN2006", "01MAY2007") DMI (dates) date (date = "LC_ALL", locale = "English_American states .1252") Date & lt; -) [1] "2006-08-01 UTC" "2006-12-01 UTC" 2006-06-01 UTC "" 2007-05-01 UTC "
The same task # I.e. date
as.Date (date, format = "% d% b% Y") # [1] "After the Reset, 08-01 "" 2006-12-01 "" 2006- 06-01 "" 2007-05-01 "
Comments
Post a Comment