Given an n by m tibble with numeric values. How do you calculate row and column totals for the tibble.
Here is a reprex with a sample tibble:
library(tidyverse)
df <- tibble(names=c('a','b','c','d','e'),x = 1:5, y =5:1, z=2:6)
df
#> # A tibble: 5 x 4
#> names x y z
#> <chr> <int> <int> <int>
#> 1 a 1 5 2
#> 2 b 2 4 3
#> 3 c 3 3 4
#> 4 d 4 2 5
#> 5 e 5 1 6
Created on 2020-06-09 by the reprex package (v0.3.0)
Given this tibble I would like efficiently use tidyverse functions to achieve the following output:
I have been able to accomplish this with a combination of gathers and spreads and joins, but I'm still fairly new to R and the tidyverse and wanted to see if there was a more efficient way to accomplish this.