format feature not working with mutate function

activity <- activity %>% 
rename( date = "activity_date") %>%
mutate(date = as.Date(date, format = "%m/%d/%Y"))

head(activity)

I run this code. activity is name assigned to the dailyActivity_merged.csv file in the Fitbit bellabeat dataset.
I was working with the Bellabeat dataset but the format function was not working in the function. Please help me where is the mistake.
The dataset is available on Kaggle.

as.Date is for going from character to date, the format is a way to specify the way the character representation looks from which to build the standard date. In R the date class on which calculations can be made is printed in a standard format.

Its important to distinguish between calculation and reporting. Just as we want to use numerics for calculation and high precision, we often want to print them ina report to a fixed precision. Similarly for a date. If we want to represent the date a particular way in a report then we format them into a character representation.

The question is therefore; is your goal at this stage to calculate, or are you ready to produce a report ?

1 Like

I am at the calculative stage. Thanks for this guidance. It was helpful.
And if i were at the report stage, how would it have been done to generate the same format date printing in all tables of the dataset?
Please help.
thanks a lot.

you can use format() function to go to character for example

(df_ <- data.frame(std_date = Sys.Date()))

library(tidyverse)

(df_2 <- mutate(df_,
                mydate_formatted_year_wordmonth = format(std_date,"%Y-%B"),
                mydate_formatted_just_day = format(std_date,"%d")
       ))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.