Hello! I am very novice and trying to learn R. Working on the Google Analytics cert with the Fitbit data. I am wanting to convert the date info in activity and sleep data. I have spent time reading a number of articles and attempting different things- all are yielding errors.
Here is what I have loaded:
install packages and load libraries
install.packages("tidyverse")
install.packages("janitor")
install.packages("lubridate")
library(tidyverse)
library(lubridate)
library(janitor)
#Load the data we plan to use and create objects for it
activity <- read.csv("dailyActivity_merged.csv")
sleep <- read.csv("sleepDay_merged.csv")
weight <- read.csv("weightLogInfo_merged.csv")
#I like the format of the ActivityDate column but its in character and I want it to be Date format but have been unsuccessful at changing it. I have tried using mutate and as.Date. I am at a loss on how to change it would appreciate any guidance.
activity2 <- select (activity,-c(TrackerDistance, LoggedActivitiesDistance))
activity2 %>%
mutate(activity2, ActivityDate= Date(mdy)/ weekdays(Date, abbreviate = FALSE)) %>%
rename(date = ActivityDate)
This is the errors:
as.Date(ActivityDate, format)
Error in as.Date(ActivityDate, format) : object 'ActivityDate' not found
as.Date(activity2, format)
Error in as.Date.default(activity2, format) :
do not know how to convert 'activity2' to class “Date”
activity2 <- as.Date(c(ActivityDate))
Error in as.Date(c(ActivityDate)) : object 'ActivityDate' not found
mutate(activity2, ActivityDate= Date(mdy)/ weekdays(Date, abbreviate = FALSE)) %>%
- rename(date = ActivityDate)
Error inmutate()
:
! Problem while computingActivityDate = Date(mdy)/weekdays(Date, abbreviate = FALSE)
.
Caused by error inrep.int()
:
! invalid type (closure) for 'times' (must be a vector)
Runrlang::last_error()
to see where the error occurred.
#I need help changing the SleepDay column. It is in a date time format and I just want the Date in mdy format without time.
sleep %>%
separate (col= SleepDay, c("date", "sleep_time"), sep="")
mutate(date = mdy(date), week_day = weekdays(SleepDate)) %>%
select(-"sleep_time")
This is the error:
separate (col= SleepDay, c("date", "sleep_time"), sep="")
Error in UseMethod("separate") :
no applicable method for 'separate' applied to an object of class "character"
mutate(date = mdy(date), week_day = weekdays(SleepDate)) %>%
- select(-"sleep_time")
Error in as.character(x) :
cannot coerce type 'closure' to vector of type 'character'
In addition I want to be able to add a weekdays column from the date and I have looked at how someone else was able to get it to work but when I copied the format I got errors. I dont know what Im doing wrong. I tried using the weekdays function and just get errors, If anyone can help guide me with how to chage the date column info in addition to adding a column for weekdays that would be amazing!!
Thank you