Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Change date format in r with as.Date [duplicate]

$
0
0

This question already has an answer here:

I have been given a big dataset, where one column is the date in factor format. I want to change this to date format, and uses as.Date

ID <- c(1,2,3,4,5)
dates <- as.factor(c('20OCT2008:00:00:00', '18NOV2008:00:00:00', '05JUN2009:00:00:00', '03MAY2009:00:00:00', '10APR2009:00:00:00'))
df  <- data.frame(ID, dates)

df$dates = as.Date(df$dates, format='%d%b%Y:%H:%M:%S')

df
  ID      dates
1  1       <NA>
2  2 2008-11-18
3  3 2009-06-05
4  4       <NA>
5  5 2009-04-10

However, only some dates are changed, while the rest occurs as NA. Any ideas to how I can transform all dates successfully or why my NAs occure?

Thx!


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>