How to find the sum of non-missing values in an R data frame column?

R ProgrammingServer Side ProgrammingProgramming

To find the sum of non-missing values in an R data frame column, we can simply use sum function and set the na.rm to TRUE. For example, if we have a data frame called df that contains a column say x which has some missing values then the sum of the non-missing values can be found by using the command sum(df$x,na.rm=TRUE).

Example1

Consider the below data frame −

 Live Demo

x1<-sample(c(NA,2,3),20,replace=TRUE)
x2<-sample(c(NA,5),20,replace=TRUE)
df1<-data.frame(x1,x2)
df1

Output

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

Finding the sum of non-missing values in columns x1 and x2 −

sum(df1$x1,na.rm=TRUE)

[1] 43

sum(df1$x2,na.rm=TRUE)

[1] 55

Example2

 Live Demo

y1<-sample(c(NA,rpois(1,2)),20,replace=TRUE)
y2<-sample(c(NA,rpois(2,8)),20,replace=TRUE)
df2<-data.frame(y1,y2)
df2

Output

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

Finding the sum of non-missing values in columns y1 and y2 −

sum(df2$y1,na.rm=TRUE)

[1] 39

sum(df2$y2,na.rm=TRUE)

[1] 66
raja
Published on 06-Mar-2021 12:22:32
Advertisements