1

It is required to convert an array column from the Factor type to the Date type.

First my attempt to do that with function anytime:

library(anytime)
anydate(trash8$DMY)

Second one I work with as.Date:

as.Date(trash8$DMY, format="%d-%m-%Y")

After first and secong attempt I have one result - all date took the form - NA

   [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  [27] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  [53] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
  [79] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [105] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [131] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [157] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [183] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [209] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
 [235] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

Can u help me to understand, what did I do wrong? Or , maybe, u know another attempts?!

Actual values:

dput(head(trash8$DMY))
structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("01.01.16", "01.02.16", 
"01.02.17", "01.02.18", "01.02.19", "01.03.16", "01.03.17", "01.03.18", 
"01.03.19", "01.04.16", "01.04.17", "01.04.18", "01.04.19", "01.05.16", 
"01.05.17", "01.05.18", "01.05.19", "01.06.16", "01.06.17", "01.06.18", 
"01.07.16", "01.07.17", "01.07.18", "01.08.16", "01.08.17", "01.08.18", 
"01.09.16", "01.09.17", "01.09.18", "01.10.16", "01.10.17", "01.10.18", 
"01.11.16", "01.11.17", "01.11.18", "01.12.16", "01.12.17", "01.12.18", 
"02.01.16", "02.01.17", "02.01.18", "02.01.19", "02.02.16", "02.02.17", 
"02.02.18", "02.02.19", "02.03.16", "02.03.17", "02.03.18", "02.03.19", 
"02.04.16", "02.04.17", "02.04.18", "02.04.19", "02.05.16", "02.05.17", 
"02.05.18", "02.05.19", "02.06.16", "02.06.17", "02.06.18", "02.07.16", 
"02.07.17", "02.07.18", "02.08.16", "02.08.17", "02.08.18", "02.09.16", 
"02.09.17", "02.09.18", "02.10.16", "02.10.17", "02.10.18", "02.11.16", 
"02.11.17", "02.11.18", "02.12.16", "02.12.17", "02.12.18", "03.01.16", 
"03.01.17", "03.01.18", "03.01.19", "03.02.16", "03.02.17", "03.02.18", 
"03.02.19", "03.03.16"), class = "factor")
  • `as.Date` should work with `factor`. Please show the actual values. It could be that your format is wrong – akrun May 07 '19 at 08:59
  • 1
    Welcome to Stack Overflow! You may want to read through [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). As it is, without seeing your data, it will be hard for anyone to help you. Please edit your question to include the output from `dput(head(trash8$DMY))` so we can see the format, as @akrun says. – duckmayr May 07 '19 at 09:00
  • probably you need to change the `format` argument in `as.Date` – davide May 07 '19 at 09:00
  • The `format` should be `"%d.%m.%y"` – akrun May 07 '19 at 09:09
  • Did you try using the `parse_date_time` function from the lubridate package? So far this function has been the most helpful in all my projects to convert vectors to dates. – Emmanuel Daveau May 07 '19 at 09:12

1 Answers1

1

Based on the dput data, the format should be different

trash8$DMY <- as.Date(trash8$DMY, format="%d.%m.%y")
trash8$DMY
#[1] "2016-01-01" "2016-01-01" "2016-01-01" "2016-01-01" "2016-01-01" "2016-01-01"
class(trash8$DMY)
#[1] "Date"
akrun
  • 874,273
  • 37
  • 540
  • 662