Find the sum of a column values based on another numerical column in R.

R ProgrammingServer Side ProgrammingProgramming

To find the sum of a column values based on another numerical column in R, we can use with function and define the sum by subsetting the column with the help of single square brackets.

For Example, if we have a data frame called df that contains two columns say X and Y then we can find the sum of values in X when Y is greater than 10 by using the command with the following −

(df,sum(X[Y10]))

Example 1

Following snippet creates a sample data frame −

x1<-rpois(20,2)
y1<-rpois(20,2)
df1<-data.frame(x1,y1)
df1

The following dataframe is created

 x1 y1
1  1 1
2  0 1
3  0 1
4  2 3
5  3 1
6  0 2
7  7 2
8  3 1
9  2 3
10 2 1
11 3 5
12 3 2
13 4 2
14 0 4
15 1 5
16 2 1
17 3 3
18 2 0
19 3 1
20 1 2

To find the sum of y1 values when x1 is between 1 and 4 on the above created data frame, add the following code to the above snippet −

x1<-rpois(20,2)
y1<-rpois(20,2)
df1<-data.frame(x1,y1)
with(df1,sum(y1[x1>1 & x1<4]))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 23

Example 2

Following snippet creates a sample data frame −

x2<-rnorm(20)
y2<-rnorm(20)
df2<-data.frame(x2,y2)
df2

The following dataframe is created

          x2        y2
1   1.14755939  0.2739985
2   0.33167239 -0.2753514
3  -0.01889732  2.0004839
4  -0.21294107 -1.2277250
5  -1.01230915 -0.4567277
6   0.30736328  0.8563572
7   0.59352845  0.7922568
8  -1.52657337  0.6147363
9   1.43228181 -0.7891716
10  0.15651466 -1.0415412
11  0.01792464 -0.3184454
12 -0.39428864  1.8005928
13 -0.48033841 -1.2787737
14 -0.51845529  0.2815327
15  0.33342239 -0.1313864
16  0.80461529 -0.2456082
17  0.30222411 -1.0134336
18 -0.83910609 -0.8805410
19 -0.06040907 1.4582650
20  0.12855851 -0.3424985

To find the sum of y2 values when x2 is between 1 and 1.2 on the above created data frame, add the following code to the above snippet −

x2<-rnorm(20)
y2<-rnorm(20)
df2<-data.frame(x2,y2)
with(df2,sum(y2[x>21 & x2<1.2]))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 0.2739985

Example 3

Following snippet creates a sample data frame −

x3<-sample(0:9,20,replace=TRUE)
y3<-sample(0:9,20,replace=TRUE)
df3<-data.frame(x3,y3)
df3

The following dataframe is created

 x3 y3
1  9 0
2  2 3
3  2 1
4  1 2
5  2 0
6  1 6
7  3 1
8  1 0
9  9 5
10 6 9
11 1 5
12 6 6
13 7 3
14 9 7
15 9 8
16 5 8
17 0 1
18 6 9
19 3 4
20 1 3

To find the sum of y3 values when x3 is between 8 and 10 on the above created data frame, add the following code to the above snippet −

x3<-sample(0:9,20,replace=TRUE)
y3<-sample(0:9,20,replace=TRUE)
df3<-data.frame(x3,y3)
with(df3,sum(y3[x>38 & x3<10]))

Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 20
raja
Updated on 02-Nov-2021 11:16:35

Advertisements