Factor Column for Date

Asked

Viewed 102 times

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 V1 column to date?

I do it:

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

2 answers

1

I would do so:

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

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

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

1

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.

  • Thank you, Marcus, but it continues to appear NA s. My historical series is bigger. I edited it in the question.

  • It is a very large txt file. Several dates are fixed and others continue to appear.

  • The code I passed solves the problem posed in the question, for the informed data frame. I can’t evaluate the problem of NA appearing in some other vector position if I don’t have access to this position.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.