Bellabeat-sum function issue

Hi all,
I am working on the public dataset of Bellabeat and I face an issue with the sum function:
When I use the below command, only one unique value is shown:
daily_activity%>%
mutate(ActiveDistance=sum(daily_activity$VeryActiveDistance,daily_activity$ModeratelyActiveDistance,daily_activity$LightActiveDistance),
ActiveMinutes=sum(daily_activity$VeryActiveMinutes,
daily_activity$FairlyActiveMinutes,daily_activity$LightlyActiveMinutes))%>%
group_by(daily_activity$Id)

Do I miss something?
Many thanks in advance for your support.
Panos

Omit the name of the data fram daily _activity which is implied by the pipe already. Adding it converts the action to refer to the entire column than just the current row.

What is the meaning of mutate function without referring to a specific table or tibble?
Anyway I tried what you suggested and an error message is shown as I expected.
Thank you for your response

is equivalent to

mutate(daily_activity, ActiveDistance=sum(

i.e. the pipe symbol %>% in this case is setting the data.frame to use with the mutate.

The sum() function adds all the values in a column or multiple columns, resulting in just one number. The simplest way to get the sum for each row is:

daily_activity %>% 
mutate(ActiveDistance = VeryActiveDistance + ModeratelyActiveDistance + LightActiveDistance)

You can also use rowSums():

daily_activity %>% 
mutate(ActiveDistance = rowSums(across(c(VeryActiveDistance, ModeratelyActiveDistance, LightActiveDistance))))

For less typing, select the columns that end in Distance

daily_activity %>% 
mutate(ActiveDistance = rowSums(across(ends_with("Distance"))))

As usual, there are many other solutions, such as sum() with rowwise() and c_across()

2 Likes

This topic was automatically closed 42 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.