Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

summing multiple columns in an R data-frame quickly [duplicate]

Tags:

dataframe

r

dplyr

I have a data frame like mtcars, and a string vector of column names such as c("mpg", "cyl", "disp", "hp", "drat") , and I would like to sum together all of the columns into a new one.

i would normally use something like

mtcars %>% transmute(new_col = mpg + cyl + disp + hp + drat)

   new_col
1   300.90
2   300.90
3   231.65
4   398.48
5   564.85
6   356.86
7   630.51

However, this becomes very tedious when you have 100s of column names, stored in a vector.

So my question is, is there a way of summing together lots of columns, where the column names are held in a vector of strings?

like image 679
user438383 Avatar asked Aug 29 '18 10:08

user438383


3 Answers

Use rowSums function.

colnms=c("mpg", "cyl", "disp", "hp", "drat")
mtcars$new_col<-rowSums(mtcars[,colnms])
like image 75
RSK Avatar answered Nov 20 '22 00:11

RSK


Here's an alternative approach using tidyverse:

library(tidyverse)

# input columns of interest
cols = c("mpg", "cyl", "disp", "hp", "drat")

mtcars %>% 
  group_by(id = row_number()) %>%  # for each row
  nest(cols) %>%                   # nest selected columns
  mutate(SUM = map_dbl(data, sum)) # calculate the sum of those columns

# # A tibble: 32 x 3
#      id data               SUM
#   <int> <list>           <dbl>
# 1     1 <tibble [1 x 5]>  301.
# 2     2 <tibble [1 x 5]>  301.
# 3     3 <tibble [1 x 5]>  232.
# 4     4 <tibble [1 x 5]>  398.
# 5     5 <tibble [1 x 5]>  565.
# 6     6 <tibble [1 x 5]>  357.
# 7     7 <tibble [1 x 5]>  631.
# 8     8 <tibble [1 x 5]>  241.
# 9     9 <tibble [1 x 5]>  267.
# 10    10 <tibble [1 x 5]>  320.
# # ... with 22 more rows

The output here is a data frame containing the row id (id), the data used at each row (data) and the calculated sum (SUM).

You can get a vector of the calculated SUM if you add ... %>% pull(SUM).

like image 35
AntoniosK Avatar answered Nov 20 '22 00:11

AntoniosK


Using Base function apply

mtcars$NewCol <- as.numeric(apply(mtcars[,1:5], 1, sum))
head(mtcars)

                   mpg cyl disp  hp drat    wt  qsec vs am gear carb NewCol
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 300.90
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 300.90
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1 231.65
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1 398.48
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2 564.85
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1 356.86
like image 33
Sri Pallavi Avatar answered Nov 20 '22 01:11

Sri Pallavi