The rowSums function (as Greg mentions) will do what you want, but you are mixing subsetting techniques in your answer, do not use "$" when using "[]", your code should look something more like:
data$new <- rowSums( data[,43:167] )
If you want to use a function other than sum, then look at ?apply for applying general functions accross rows or columns.
I came here hoping to find a way to get the sum across all columns in a data table and run into issues implementing the above solutions. A way to add a column with the sum across all columns uses the cbind function:
cbind(data, total = rowSums(data))
This method adds a total column to the data and avoids the alignment issue yielded when trying to sum across ALL columns using the above solutions (see the post below for a discussion of this issue).
You can also use this function adorn_totals from janitor package.
You can sum the columns or the rows depending on the value you give to the arg: where.
Example:
tibble::tibble(
a = 10:20,
b = 55:65,
c = 2010:2020,
d = c(LETTERS[1:11])) %>%
janitor::adorn_totals(where = "col") %>%
tibble::as_tibble()
Result:
# A tibble: 11 x 5
a b c d Total
<int> <int> <int> <chr> <dbl>
1 10 55 2010 A 2065
2 11 56 2011 B 2067
3 12 57 2012 C 2069
4 13 58 2013 D 2071
5 14 59 2014 E 2073
6 15 60 2015 F 2075
7 16 61 2016 G 2077
8 17 62 2017 H 2079
9 18 63 2018 I 2081
10 19 64 2019 J 2083
11 20 65 2020 K 2085
expr min lq mean median uq max neval
apply_func 207.84661 260.34475 280.14621 279.18782 294.85119 354.1821 100
r_sum 10.76534 11.53194 13.00324 12.72792 14.34045 16.9014 100
As you notice that the mean time for the rowSums function is 21 times smaller than the mean time of the apply function. You will find that the difference in the elapsed time may be more significant if the matrix has too many columns.
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: