I am relatively new to R, but this is the first time I have had to deal with date conversions. I read in my data from a CSV (using read.table()), but I shorted the data to highlight my issue. When read into R, the Date field is character.
Simply, most of my dates get coerced correctly, except for a few instances. The example below will hopefully show you what is going on.
# my attempt to coerce the date -- uses the stringr package
prods.all$Date2 <- as.Date(str_sub(prods.all$Date, 1,
str_locate(prods.all$Date, " ")[1]-1),
"%m/%d/%Y")
# grab two rows to highlight my issue
temp <- prods.all[c(1925:1926), c(1,8)]
> temp
Date Date2
1925 10/9/2009 0:00:00 2009-10-09
1926 10/15/2009 0:00:00 0200-10-15
As you can see, the year of some of the dates is inaccurate. The pattern seems to occur when the day is double digit.
I have looked through a couple of books and tried to Google a better way, but everything seems to suggest that my data are not formatted correctly on input.
Given how powerful R is, I figure that there is a very easy way to force my column to be valid dates and that I am overlooking a very obvious solution.
Any help you can provide will be greatly appreciated.
question from:
https://stackoverflow.com/questions/4310326/convert-character-to-date-in-r 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…