Factor Column for Date

2

My data frame

    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))

How do I convert the V1 column to date?

I do this:

x$V1 <- as.Date(x$V1, format="%d-%m-%Y") , but exits <NA> .

    
asked by anonymous 04.04.2017 / 20:14

2 answers

1

The date format is not correct. Use

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

More information about date format can be found using ?strptime .

    
04.04.2017 / 20:20
0

I would do so:

library(lubridate)
dmy(as.character(x$V1))

Note that because column V1 is loaded as factor in R, I had to convert to character first, ideally you would have read its base with the stringAsFactors = FALSE argument, or use the readr functions that do this by default.

The lubridate has several functions to convert text to date: dmy indicates that dates are in day-month-year format, but has function for all permutations ymd , mdy , etc. It also has function for when the dates are accompanied by the time: dmy_hms , for example.

    
05.04.2017 / 16:12