Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

lubridate - Convert character to Date in R

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You may be overcomplicating things, is there any reason you need the stringr package?

 df <- data.frame(Date = c("10/9/2009 0:00:00", "10/15/2009 0:00:00"))
 as.Date(df$Date, "%m/%d/%Y %H:%M:%S")

[1] "2009-10-09" "2009-10-15"

More generally and if you need the time component as well, use strptime:

strptime(df$Date, "%m/%d/%Y %H:%M:%S")

I'm guessing at what your actual data might look at from the partial results you give.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...