- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the sum of non-missing values in an R data frame column?
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 −
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
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
- Related Articles
- How to find the sum of column values of an R data frame?
- How to find the percentage of missing values in each column of an R data frame?
- Find the number of non-missing values in each column by group in an R data frame.\n
- How to find the number of non-empty values in an R data frame column?
- Find the number of non-missing values in each group of an R data frame.
- How to find the sum of squared values of an R data frame column?
- How to find the percentage of missing values in an R data frame?
- How to replace missing values with median in an R data frame column?
- Find the frequency of unique values and missing values for each column in an R data frame.
- How to find the number of groupwise missing values in an R data frame?
- How to replace missing values in a column with corresponding values in other column of an R data frame?
- How to subset non-duplicate values from an R data frame column?
- How to find the sum of every n values if missing values exists in the R data frame?
- How to find the sum of values based on key in other column of an R data frame?
- How to find the unique values in a column of an R data frame?

Advertisements