Creating a new vector from multiple operations

Hi I want to save the result of some operations in a new vector. Let´s say I have this tibble

library(tidyverse)

my_data <- tibble(
                  Var_1 = c(900, 1500, 350, 1200, 750, 100,125,250,300),
                  Var_2 = c(385, 988, 150, 355,555, 900,20, 25, 500),
                  Var_3 = c(2.2, 3.1, 8.2, 4.2, 5.3, 6.8, 12, 25, 1))

And I want to perform some operations like

sum(Var_1)
sum(Var_2)
sum(Var_3)

How I create a new vector to save the result of this 3 operations?
Thanks in advance

#base
(new_vec1 <- c(sum(my_data$Var_1) ,
              sum(my_data$Var_2),
              sum(my_data$Var_3)))
#dplyr
(new_vec2 <- my_data %>% summarise_all(sum) %>% unlist)
1 Like

Many thanks,
If the data came from differents dataframes is still posible to use the dplyr way?

you could do it for as many dataframes as you like, and then combine the results together with c()
you could iterate with the purrr package functions if needed.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.