How to find the sum of column values of an R data frame?


An R data frame contain columns that might represent a similar type of variables; therefore, we might want to find the sum of the values for each of the columns and make a comparison based on the sum. This can be done with the help of sum function but first we need to extract the columns to find the sum.

Example

Consider the below data frame −

> set.seed(1)
> x1<-rnorm(20)
> x2<-rnorm(20,0.5)
> x3<-rnorm(20,1)
> x4<-rnorm(20,1.5)
> x5<-rnorm(20,2)
> x6<-rnorm(20,2.5)
> x7<-rnorm(20,3)
> df<-data.frame(x1,x2,x3,x4,x5,x6,x7)
> df
x1 x2 x3 x4 x5 x6 x7
1 -0.62645381 1.41897737 0.83547640 3.9016178 1.4313313 1.879633 2.494043
2 0.18364332 1.28213630 0.74663832 1.4607600 1.8648214 2.542116 4.343039
3 -0.83562861 0.57456498 1.69696338 2.1897394 3.1780870 1.589078 2.785421
4 1.59528080 -1.48935170 1.55666320 1.5280022 0.4764332 2.658029 2.820443
5 0.32950777 1.11982575 0.31124431 0.7567268 2.5939462 1.845415 2.899809
6 -0.82046838 0.44387126 0.29250484 1.6887923 2.3329504 4.267287 3.712666
7 0.48742905 0.34420449 1.36458196 -0.3049586 3.0630998 3.216707 2.926436
8 0.73832471 -0.97075238 1.76853292 2.9655549 1.6958161 3.410174 2.962366
9 0.57578135 0.02184994 0.88765379 1.6532533 2.3700188 2.884185 2.318340
10 -0.30538839 0.91794156 1.88110773 3.6726117 2.2670988 4.182176 2.675730
11 1.51178117 1.85867955 1.39810588 1.9755095 1.4574800 1.864264 3.060160
12 0.38984324 0.39721227 0.38797361 0.7900536 3.2078678 2.038355 2.411106
13 -0.62124058 0.88767161 1.34111969 2.1107264 3.1604026 3.932282 3.531496
14 -2.21469989 0.44619496 -0.12936310 0.5659024 2.7002136 1.849304 1.481606
15 1.12493092 -0.87705956 2.43302370 0.2463666 3.5868335 2.292619 3.306558
16 -0.04493361 0.08500544 2.98039990 1.7914462 2.5584864 2.107192 1.463550
17 -0.01619026 0.10571005 0.63277852 1.0567081 0.7234078 2.180007 2.699024
18 0.94383621 0.44068660 -0.04413463 1.5011054 1.4267346 2.220887 2.471720
19 0.82122120 1.60002537 1.56971963 1.5743413 0.7753874 2.994188 2.347905
20 0.59390132 1.26317575 0.86494540 0.9104791 1.5265994 2.322670 2.943103

Finding the sum of all columns one by one −

> sum(df$x1)
[1] 3.810478
> sum(df$x2)
[1] 9.87057
> sum(df$x3)
[1] 22.77594
> sum(df$x4)
[1] 32.03474
> sum(df$x5)
[1] 42.39702
> sum(df$x6)
[1] 52.27657
> sum(df$x7)
[1] 55.65452

Let’s have a look at one more example −

> y1<-1:10
> y2<-11:20
> y3<-rep(c(1,2,3,4,5),times=2)
> df_y<-data.frame(y1,y2,y3)
> df_y
  y1 y2 y3
 1 1 11 1
 2 2 12 2
 3 3 13 3
 4 4 14 4
 5 5 15 5
 6 6 16 1
 7 7 17 2
 8 8 18 3
 9 9 19 4
10 10 20 5
> sum(df_y$y1)
[1] 55
> sum(df_y$y2)
[1] 155
> sum(df_y$y3)
[1] 30

Updated on: 11-Aug-2020

371 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements