0

I have the following data file called "data_2":

Person Weight Height
A      55     155
B      65     165
C      75     175

I wanna use the Summarise(across(where))-command in order to generate the total weight and the weight for each person. This is what I have tried until now.

data_2 <- read_excel("data_2.xlsx", sheet = 2)


data_2 %>%
summarise(across(where(is.numeric), sum))

Unfortunately, this don't work correctly. Does anyone have an idea on how to solve this?

Expected output:

Person Weight 
A      55     
B      65     
C      75
Total  195   
John Doe
  • 37
  • 8

1 Answers1

1

Try this:

library(dplyr)
#Code
newdf <- data_2 %>%
  bind_rows(data_2 %>% select(-1) %>%
              summarise_all(sum) %>% mutate(Person='Total'))

Output:

  Person Weight Height
1      A     55    155
2      B     65    165
3      C     75    175
4  Total    195    495

Or using your code:

#Code 2
newdf <- data_2 %>% 
  bind_rows(data_2 %>% summarise(across(where(is.numeric),sum)) %>%
              mutate(Person='Total')) %>% select(-Height)

Output:

  Person Weight
1      A     55
2      B     65
3      C     75
4  Total    195
Duck
  • 39,058
  • 13
  • 42
  • 84
  • But I don't want to print the Height column. Only Weight. – John Doe Nov 20 '20 at 13:54
  • @JohnDoe Check the last code in the solution! – Duck Nov 20 '20 at 13:55
  • @JohnDoe Or try `newdf <- data_2 %>% select(-Height) %>% bind_rows(data_2 %>% summarise(across(starts_with('Weight'),sum)) %>% mutate(Person='Total')) ` – Duck Nov 20 '20 at 13:56
  • That solution works, however if I have a longer file with says N persons, it will only show the 10 first persons (Person A -J) s tibble. It will generate a 15 x 1 tibble but only the 10 first are visible. How can I show the other observations as well(Person K - N, + Total)? – John Doe Nov 20 '20 at 14:22
  • @JohnDoe In the new dataframe it will be all persons you have. If you want to print on console all people try this `data_2 %>% select(-Height) %>% bind_rows(data_2 %>% summarise(across(starts_with('Weight'),sum)) %>% mutate(Person='Total')) %>% data.frame()` – Duck Nov 20 '20 at 14:24
  • '''newdf <- data_2 %>% bind_rows(my_data_2 %>% summarise(across(where(is.numeric),sum)) %>% mutate(Person='Total')) %>% select(-Height)''' – John Doe Nov 20 '20 at 14:26
  • I wanna use that command since it uses the summarise(across(where)). Not the other one you suggested. – John Doe Nov 20 '20 at 14:27
  • @JohnDoe In that case try `newdf <- data_2 %>% bind_rows(data_2 %>% summarise(across(where(is.numeric),sum)) %>% mutate(Person='Total')) %>% select(-Height) %>% data.frame() ` – Duck Nov 20 '20 at 14:29
  • Now it works perfectly fine. I will accept your answer. Thank you for taking the time! :) – John Doe Nov 20 '20 at 14:41
  • @JohnDoe Many thanks John :) are you still learning R (new user)? – Duck Nov 20 '20 at 14:43
  • Yes, I do. I think it's a bit hard but I progress every day. – John Doe Nov 20 '20 at 15:12
  • @JohnDoe I understand. I use to teach customized session for specific problems with R Programming. If you can be interested at any point of time, let me know :) – Duck Nov 20 '20 at 20:19