AMM
AMM

Reputation: 61

Converting a column of characters to dates

Using R and the dplyr package:

I have a column of characters that I want to be converted to dates. For example, they appear in the format 2015-10-09. I tried data_frame %>% as.Date(column, "%Y-%m-%d") but when I run it I get an error message saying:

Error in as.Date.default(., data_frame, column, "%Y-%m-%d") : do not know how to convert '.' to class “Date”

So I see that R is putting a '.' at the front but I don't know why or how to change that. How can I successfully change this column of characters to dates?

Upvotes: 1

Views: 65

Answers (1)

akrun
akrun

Reputation: 887851

We can use that in mutate because the output coming from %>% is the whole dataset instead of a single column

library(dplyr)
data_frame <- data_frame %>%
                mutate(column = as.Date(column, "%Y-%m-%d"))

Upvotes: 2

Related Questions