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:

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

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()


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.