Just for completeness. I will list other methods
no mentioned here this is different ways for do it the same thing using dplyr syntax with a matrix:
mat = matrix(1:12, ncol = 3)
library(dplyr)
mat %>% as_tibble() %>%
mutate(sum = rowSums(across(where(is.numeric))))
# A tibble: 4 x 4
V1 V2 V3 sum
<int> <int> <int> <dbl>
1 1 5 9 15
2 2 6 10 18
3 3 7 11 21
4 4 8 12 24
or c_across:
mat %>% as_tibble() %>%
rowwise() %>%
mutate(sumrange = sum(c_across(), na.rm = T))
or selecting specific column by column name:
mat %>% as_tibble() %>%
mutate( 'B1' = V1, B2 = V2) %>%
rowwise() %>%
mutate(sum_startswithB =
sum(c_across(starts_with("B")), na.rm = T))
V1 V2 V3 B1 B2 sum_startswithx
<int> <int> <int> <int> <int> <int>
1 1 5 9 1 5 6
2 2 6 10 2 6 8
3 3 7 11 3 7 10
4 4 8 12 4 8 12
by column index in this case the first column to 4th column :
mat %>% as_tibble() %>%
mutate( 'B1' = V1, B2 = V2) %>%
rowwise() %>%
mutate(SumByIndex = sum(c_across(c(1:4)), na.rm = T))
V1 V2 V3 B1 B2 SumByIndex
<int> <int> <int> <int> <int> <int>
1 1 5 9 1 5 16
2 2 6 10 2 6 20
3 3 7 11 3 7 24
4 4 8 12 4 8 28
Using Regular Expresion:
mat %>% as_tibble() %>%
mutate( 'B1' = V1, B2 = V2) %>%
mutate(sum_V = rowSums(.[grep("V[2-3]", names(.))], na.rm = TRUE),
sum_B = rowSums(.[grep("B", names(.))], na.rm = TRUE))
V1 V2 V3 B1 B2 sum_V sum_B
<int> <int> <int> <int> <int> <dbl> <dbl>
1 1 5 9 1 5 14 6
2 2 6 10 2 6 16 8
3 3 7 11 3 7 18 10
4 4 8 12 4 8 20 12
Using Apply Funcion is more handy because you can
choose sum, mean, max, min, variance and standard deviation across columns.
mat %>% as_tibble() %>%
mutate( 'B1' = V1, B2 = V2) %>%
mutate(sum = select(., V1:B1) %>% apply(1, sum, na.rm=TRUE)) %>%
mutate(mean = select(., V1:B1) %>% apply(1, mean, na.rm=TRUE)) %>%
mutate(max = select(., V1:B1) %>% apply(1, max, na.rm=TRUE)) %>%
mutate(min = select(., V1:B1) %>% apply(1, min, na.rm=TRUE)) %>%
mutate(var = select(., V1:B1) %>% apply(1, var, na.rm=TRUE)) %>%
mutate(sd = select(., V1:B1) %>% apply(1, sd, na.rm=TRUE))
V1 V2 V3 B1 B2 sum mean max min var sd
<int> <int> <int> <int> <int> <int> <dbl> <int> <int> <dbl> <dbl>
1 1 5 9 1 5 16 4 9 1 14.7 3.83
2 2 6 10 2 6 20 5 10 2 14.7 3.83
3 3 7 11 3 7 24 6 11 3 14.7 3.83
4 4 8 12 4 8 28 7 12 4 14.7 3.83
Note: the var and sd same output is not an error is because the data is generated linearly 1:12
you can verify calculating the values of the first columns:
> sd(c(1,5,9,1))
[1] 3.829708
> sd(c(2,6,10,2))
[1] 3.829708