Problem grouping my data by a column in the data frame

Can anyone tell me how I can fix the following code as followed:

daily_totals<- merged_daily_activity %>% 
  + group_by(ActivityDay) %>% 
  +summarise(total_steps= sum(TotalSteps.x, na.rm = TRUE), total_calories= sum(Calories.x, na.rm = TRUE), total_sleep_hours= sum(TotalSleepRecords, na.rm = TRUE))
head(daily_totals)

Error: object 'ActivityDay' not found

The above is the error that I'm getting. However, I do have a column name "ActivityDay." Can someone give me some answers as to what's wrong.

I think you're mixing ggplot2 syntax (where layers are added with +) with the magritter %>% (or base R |>) pipe. Your code should be something more like:

daily_totals <- merged_daily_activity %>% 
    group_by(ActivityDay) %>% 
    summarise(
        total_steps= sum(TotalSteps.x, na.rm = TRUE),
        total_calories= sum(Calories.x, na.rm = TRUE),
        total_sleep_hours= sum(TotalSleepRecords, na.rm = TRUE)
    )
head(daily_totals)

This is provided that ActivityDay is a column in your merged_daily_activity dataframe. The error you are getting is due to using the column as a symbol outside of the scope or "environment" of your dataframe, I believe due to the combined syntax.

1 Like

I followed your advice and it worked! Thanks!