Lubridate, change factor to date

Oh sorry. It was a bit late when I wrote that and I see now I was too brief.

I needed a factor to work with so I created a factor called xx with :

xx  <-  as.factor("2020-10-12")

Do

class(xx)

or

str(xx)

to see that is, in fact a factor.

We cannot directly convert a factor to a date. So first we convert the factor value to a character value using as.character(). Then we apply the ymd() function to convert to date format

to make it a little simpler

## Create factor data
xx  <-  as.factor("2020-10-12")

## load needed library
library(lubridate)

## Convert factor to character
 yy  <-  as.character(xx)

## convert character data to date
 yy  <-  as.character(xx)

Eh voilà.

Assuming you have a column of data called "xx" in a data.frame or a tibble called "dat1", to make it easy to see what is happening we can do this:

## Convert to character data
yy   <-  as.character(dat1$xx)

## Covert to date data and replace the old format data with the new date format data  in the tibble or data.frame
dat1$xx  <-  ymd(yy)

Once you are more familiar with R it is just more convenient to it in one line as in:

dat1$xx  <-  ymd(as.character(dat1$xx)

So, assuming your data column is "Activity2015_2017$ActivityDate" and changing to the lubridate function dmy() to match for data format this should work:

Activity2015_2017$ActivityDate  <-  dmy(as.character(Activity2015_2017$ActivityDate))

You might find these links useful.

Good luck and welcome to R