Reputation: 1
I am doing the Bellabeat case study from the google analytics course. I am doing it in R and noticed that the date format was in character format rather than "Date" so I tried to use the mutate()
function to convert it to a date format.
The dataframe is called sleep
The code looks as:
sleep <- sleep %>%
mutate(date = ymd(Date))
But instead of converting it to a date format it went from this:
Id Date TotalSleepRecords TotalMinutesAsleep TotalTimeInBed
1 1503960366 4/12/2016 1 327 346
2 1503960366 4/13/2016 2 384 407
3 1503960366 4/15/2016 1 412 442
4 1503960366 4/16/2016 2 340 367
5 1503960366 4/17/2016 1 700 712
6 1503960366 4/19/2016
to,
> head(sleep)
Id Date TotalSleepRecords TotalMinutesAsleep TotalTimeInBed date
1 1503960366 <NA> 1 327 346 <NA>
2 1503960366 <NA> 2 384 407 <NA>
3 1503960366 <NA> 1 412 442 <NA>
4 1503960366 <NA> 2 340 367 <NA>
5 1503960366 <NA> 1 700 712 <NA>
6 1503960366 <NA> 1 304 320 <NA>
What exactly did I do wrong and how can I revert it?
My purpose with the dataframes I work with is to compare how exercise impacts sleep. I am not sure if I actually need to convert the date format, but many others who did the case study did it, so I figured that it might impact the analysis if I do not do it. There are several other dataframes in the package I work with. I figure I need to merge dataframes to compare columns.
Upvotes: 0
Views: 135
Reputation: 26
As mentioned in the replies, since your dates are in "mm/dd/yyyy" format you should use lubridate::mdy()
to convert them to dates.
As a general workflow tip, it's a good idea to check what the results of your operation would be before overwriting the dataframe. For example if I have
library(dplyr)
library(lubridate)
sleep <- tibble(id = c(1, 2), date = c("4/12/2016", "4/13/2016"))
then in the console I can try out the operation first:
sleep %>%
mutate(date = mdy(date))
#> id date
#> <dbl> <date>
#> 1 1 2016-04-12
#> 2 2 2016-04-13
Now that I'm satisfied my mutation has done what I want it to, I can save the results. I tend to save the results to a new dataframe:
sleep_clean <- sleep %>%
mutate(date = mdy(date))
so that I can still access the original sleep
dataframe after (thus 'reverting' to the previous version).
Upvotes: 1