0

I'm having an issue trying to format date in r... tried the following codes rdate<-as.Date(dusted$time2,"%d/%m/%y") and also the recommendations on this stackoverflow question Changing date format in R but still couldn't get it to work.

geov<-dusted geov$newdate <- strptime(as.character(geov$time2), "%d/%m/%Y")

all i'm getting is NA for the whole column for date. This are daily values, i would love if r can read them. Data available here https://www.dropbox.com/s/awstha04muoz66y/dusted.txt?dl=0

Community
  • 1
  • 1
Hammao
  • 801
  • 1
  • 9
  • 28
  • The dates in your dataset are separated by hyphens, while you have specified slashes. Another problem is you're using `%Y` which is for 4 digit years. For 2 digit years, you need to use `%y`. – ytk Feb 09 '16 at 18:10
  • Thanks @Teja, that was helpful... – Hammao Feb 10 '16 at 13:51

1 Answers1

1

To convert to date, as long as you successfully imported the data already into a data frame such as dusted or geov, and have time2 holding dates as strings resembling 10-27-06, try:

geov$time2 = as.Date(geov$time2, "%m-%d-%y")
  • equal sign = used just to save on typing. It is equivalent to <-, so you can still use <- if you prefer
  • this stores the converted dates right back into geov$time2, overwriting it, instead of creating a new variable geov$newdate as you did in your original question. This is because a new variable is not required for conversion. But if for some reason you really need a new variable, feel free to use geov$newdate
  • similarly, you also didn't need to copy dusted to a new geov data frame just to convert. It does save time for testing purposes though, just in case the conversion doesn't work you can restart from copying dusted to geov instead of having to re-import data from a file into dusted

Additional resources

  • help(strptime) for looking up date code references such as %y. On Linux, man date can reveal the date codes
clarity123
  • 1,956
  • 10
  • 16