0

My data frame is:

    x=structure(list(V1 = structure(c(33L, 35L, 36L, 37L, 39L, 4L, 
6L, 7L, 8L, 10L, 14L, 16L, 18L, 19L, 21L, 25L, 27L, 28L, 29L, 
30L, 1L, 17L, 31L, 32L, 34L, 38L, 40L, 2L, 3L, 5L, 9L, 11L, 12L, 
13L, 15L, 20L, 22L, 23L, 24L, 26L), .Label = c("1-Feb-71", "10-Feb-71", 
"11-Feb-71", "11-Jan-71", "12-Feb-71", "12-Jan-71", "13-Jan-71", 
"14-Jan-71", "15-Feb-71", "15-Jan-71", "16-Feb-71", "17-Feb-71", 
"18-Feb-71", "18-Jan-71", "19-Feb-71", "19-Jan-71", "2-Feb-71", 
"20-Jan-71", "21-Jan-71", "22-Feb-71", "22-Jan-71", "23-Feb-71", 
"24-Feb-71", "25-Feb-71", "25-Jan-71", "26-Feb-71", "26-Jan-71", 
"27-Jan-71", "28-Jan-71", "29-Jan-71", "3-Feb-71", "4-Feb-71", 
"4-Jan-71", "5-Feb-71", "5-Jan-71", "6-Jan-71", "7-Jan-71", "8-Feb-71", 
"8-Jan-71", "9-Feb-71"), class = "factor"), V2 = structure(c(1L, 
15L, 2L, 4L, 3L, 5L, 10L, 5L, 7L, 12L, 8L, 16L, 16L, 22L, 16L, 
19L, 22L, 12L, 17L, 23L, 24L, 24L, 21L, 17L, 19L, 16L, 6L, 11L, 
9L, 25L, 25L, 8L, 5L, 13L, 20L, 18L, 16L, 13L, 12L, 14L), .Label = c("7.1359", 
"7.1367", "7.1382", "7.1386", "7.1390", "7.1397", "7.1403", "7.1406", 
"7.1410", "7.1411", "7.1412", "7.1414", "7.1418", "7.1420", "7.1422", 
"7.1429", "7.1431", "7.1434", "7.1435", "7.1437", "7.1439", "7.1443", 
"7.1445", "7.1465", "ND"), class = "factor")), .Names = c("V1", 
"V2"), class = "data.frame", row.names = c(NA, -40L))

I am trying to convert column V1 to Date, but it is not working. Ive been looking some topics but it just doesnt work.

This my code:

x$V1 <- as.Date(x$V1, format="%d-%b-%y")

It works for some rows of V1 column but not for others.

Any help?

1 Answers1

1

In my version of R, the conversion in your example only works for January and not for February. I think it is related to the language. For example, in French, February is coded as fév and so Feb is not recognized. Once I did:

x$V1=gsub("Feb", "fév", x$V1)

it worked. It probably depends on which language your version of R uses.

Lamia
  • 3,845
  • 1
  • 12
  • 19
  • Instead of changing the data, you can change R locale: `loc <- Sys.getlocale("LC_TIME")` to save your locale, `Sys.setlocale("LC_TIME", "C") ` to switch to C locale, `x$V1 <- as.Date(x$V1, format="%d-%b-%y")` and `Sys.getlocale(loc)`. – xraynaud Apr 04 '17 at 19:37
  • Complicated. I change to my language and works. Now I will change for all months to see if it works for then. Thanks!! –  Apr 04 '17 at 19:39
  • @xraynaud Thanks for the suggestion! Shouldn't you do Sys.setlocale("LC_TIME",loc) at the end to revert? To OP: It probably makes more sense to change your locale to do your analysis, and then revert to the original one as mentioned in xraynaud's comment, if you have a lot of data to change. – Lamia Apr 04 '17 at 19:52
  • @lamia yes. it's `Sys.getlocale("LC_TIME",loc)` – xraynaud Apr 04 '17 at 19:58