简体   繁体   中英

strptime in R returning NA

I have a variable in my dataset called timestamp which is of the form. mydata$timestamp

2013-08-01 12:00:00 2013-08-01 12:00:00 2013-08-01 12:00:00

I want to modify these and change them to only dd-mm-yy format dates<-strptime(mydata$timestamp, format="%d:%m:%y") printing dates is resulting in only NA's. Not sure why.

Could anyone help, please?

Thanks in advance

Pascal's answer is correct, but I'm going to add to it because with all due respect to him I think you need to be aware of the mistakes you are making so you won't make them in the future.

In some programming languages there are internal dates that have associated formats and you can change that format without changing the internal representation of the date. That is probably why you phrased the question as you did, but that is not the way R works. In R you can either have dates represented as a string or as an actual date class that R understands, like Date or POSIXlt . Classes that R understands don't have any specific output format associated with them.

Your input data appears to be a string representation of a date. It appears that you want it in a different string representation. strptime() will change from a string to POSIXlt but this data type isn't formatted one way or the other. If you want to turn it back into a string, you need to use a different command. In Pascal's example, that function is format() .

Ok, so you want to use strptime() to turn it into an R date and then use format() to turn it back into a string. Fine, but you have to have the arguments right. The second argument to strptime() is a set of characters that informs the function of what the current format is. Your argument "%d:%m:%y" is not remotely similar to what your data is. That's why are getting NA . As Pascal points out, the correct format is "%Y-%m-%d %H:%M:%S" . Check the help file for strptime() to see what the symbols in the formatting strings mean.

Personally I avoid all that local time stuff that strptime() does and just use R's basic Date() class. My solution would be

dates <- format(as.Date(mydata$timestamp,format="%Y-%m-%d %H:%M:%S"),format="%d-%m-%y")

Notice that that format argument in as.Date() informs the function of what the incoming data format is and the format argument in format() tells it what you want the outgoing format to be.

If you want dd-mm-yy format, you need format(mydata$timestamp, "%d-%m-%y") . For example:

x <- strptime(c("2006-01-08 10:07:52", "2006-08-07 19:33:02"), "%Y-%m-%d %H:%M:%S", tz = "EST5EDT")
[1] "2006-01-08 10:07:52 EST" "2006-08-07 19:33:02 EDT"

format(x, "%d-%m-%y")
[1] "08-01-06" "07-08-06"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM