I have a tibble
(I'm already using tidyverse
), but since it's large, let's use a slightly modified iris
dataset:
iris %>%
as_tibble() %>%
select(-5)
I want to create another column that is the square root of the sum of the squares of these columns (which are all dbl
, as you may notice). However, this method should generalize for any number of columns and with any name, not only because the original dataset has more columns, but also because it's more elegant.
This code works, but is specific to these columns with these names:
iris %>%
as_tibble() %>%
select(-5) %>%
mutate(linear = sqrt(Sepal.Length^2+Sepal.Width^2+Petal.Length^2+Petal.Width^2)) %>%
arrange(linear)
This gives the expected output:
> iris %>%
+ as_tibble() %>%
+ select(-5) %>%
+ mutate(linear = sqrt(Sepal.Length^2+Sepal.Width^2+Petal.Length^2+Petal.Width^2)) %>%
+ arrange(linear)
# A tibble: 150 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width linear
<dbl> <dbl> <dbl> <dbl> <dbl>
1 4.5 2.3 1.3 0.3 5.23
2 4.3 3 1.1 0.1 5.36
3 4.4 2.9 1.4 0.2 5.46
4 4.4 3 1.3 0.2 5.49
5 4.4 3.2 1.3 0.2 5.60
6 4.6 3.1 1.5 0.2 5.75
7 4.6 3.2 1.4 0.2 5.78
8 4.8 3 1.4 0.1 5.83
9 4.7 3.2 1.3 0.2 5.84
10 4.8 3 1.4 0.3 5.84
# … with 140 more rows