How to calculate mean or sum from a table data?

Traditional R method by using colSums (for colwise) and rowSums (for row-wise) for sum

df <- tibble(x = 1:2, y = 3:4, z = 5:6)
df2 <- rbind(df, colSums(df));df2 # col-wise
## # A tibble: 3 × 3
##       x     y     z
##   <dbl> <dbl> <dbl>
## 1     1     3     5
## 2     2     4     6
## 3     3     7    11
df3 <- cbind(df,rowSums(df));df3 # row-wise
##   x y z rowSums(df)
## 1 1 3 5           9
## 2 2 4 6          12

Row-wise operations by tidyverse

See (dplyr site)[https://dplyr.tidyverse.org/articles/rowwise.html]

row-wise

# does not work
df %>% mutate(m = mean(c(x, y, z)))
## # A tibble: 2 × 4
##       x     y     z     m
##   <int> <int> <int> <dbl>
## 1     1     3     5   3.5
## 2     2     4     6   3.5
# work!
df %>% rowwise() %>% mutate(m = mean(c(x, y, z)))
## # A tibble: 2 × 4
## # Rowwise: 
##       x     y     z     m
##   <int> <int> <int> <dbl>
## 1     1     3     5     3
## 2     2     4     6     4
# add one column for sum
df %>% rowwise() %>% mutate(m = mean(c(x, y, z))) %>% mutate(s=sum(c(x,y,z)))
## # A tibble: 2 × 5
## # Rowwise: 
##       x     y     z     m     s
##   <int> <int> <int> <dbl> <int>
## 1     1     3     5     3     9
## 2     2     4     6     4    12

colwise

vignette("colwise")
## starting httpd help server ... done
df %>% summarise(across(x:z,mean))
## # A tibble: 1 × 3
##       x     y     z
##   <dbl> <dbl> <dbl>
## 1   1.5   3.5   5.5