I want to create a new column that sums up values from existing columns.
Consider iris dataset:
iris1 <- iris %>% mutate(totalSepalLength = across(contains("Sepal"), sum))
This gives me a new column which is of type tibble. If I access iris1$totalSepalLength
, it does not sum row-wise but rather, gives a total sum for the two columns:
# A tibble: 150 x 2
Sepal.Length Sepal.Width
<dbl> <dbl>
1 876. 459.
2 876. 459.
3 876. 459.
4 876. 459.
5 876. 459.
6 876. 459.
7 876. 459.
8 876. 459.
9 876. 459.
10 876. 459.
# ... with 140 more rows
I have tried multiple variations of across but I can't seem to get it work:
> iris1 <- iris %>% mutate(totalSepalLength = sum(c_across(contains("Sepal"))))
> glimpse(iris1)
Rows: 150
Columns: 6
$ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.8, 4.8, 4.3, 5.8, 5.7, 5.4, 5.1, 5.7, 5.1, 5.4, 5.1, 4~
$ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.4, 3.0, 3.0, 4.0, 4.4, 3.9, 3.5, 3.8, 3.8, 3.4, 3.7, 3~
$ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.6, 1.4, 1.1, 1.2, 1.5, 1.3, 1.4, 1.7, 1.5, 1.7, 1.5, 1~
$ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1, 0.2, 0.4, 0.4, 0.3, 0.3, 0.3, 0.2, 0.4, 0~
$ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa, setosa,~
$ totalSepalLength <dbl> 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1, 1335.1,~
>
I am specifically looking for a solution that uses dplyr (mutate→across) workflow.