R Filter Dataframe with atleast N number of non-NAs

R Filter Dataframe with atleast N number of non-NAs

In this tutorial, we will learn how to filter rows of a dataframe with alteast N number of non-NA column values.

Filter R Dataframe with atleast N number of non-NAs

To filter rows of a dataframe that has atleast N non-NAs, use dataframe subsetting as shown below

resultDF = mydataframe[rowSums (is.na(mydataframe[ , 0:nc ol(mydat aframe)]) )reAm, 7y4d)a,tapfr=amc(eN=A,d6a3ta, .frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21), z = c(9, 8, NA, 74), p = c(NA, 63, NA, 2)) NA, 2)) >>mmyyddaattaaffrraammee

xx yy zzp p 1 9 4 9 NA 21 N9A4N9AN8A 63 342 N74AN21NAA7N48A 6N23A 3 7 NA NA NA 4 4 21 74 2

Now, we will filter this dataframe such that the output contains only rows with atleast 2 non-NAs.

> N = 2 > resultDF = mydataframe[rowSums (is.na(mydataframe[ , 0:nc ol(mydat aframe)]) ) resultDF

x y z p 1 9 4 9 NA 2 NA NA 8 63 4 4 21 74 2

> N = 2 > resultDF = mydataframe[rowSums(is.na(mydataframe[ , 0:ncol(mydataframe)])) resultDF

x y z p 1 9 4 9 NA 2 NA NA 8 63 4 4 21 74 2 >

Let us try with N = 3.

> N=3 > resultDF = mydataframe[rowSums (is.na(mydataframe[ , 0>:nNc=o3l(mydataframe)]) ) ncroels(umltyDdFat=afmraymdea)ta-frame[rowSums(is.na(mydataframe[ , 0:ncol(mydataframe)])) ),re]sultDF > resultDF

xx yy zzp p 141 949 24419794NNAA2 >4 4 21 74 2 >

Concluding this R Tutorial, we have learned to filter a Dataframe based on the number of non-NAs (or ofcourse NAs) in a row.

Home - Get Started

R Tutorial R Script File R Working Directory R Data Types R Variables R Operators R Vectors R Matrix

Decision Making

R Decision Making R if R if..else R if..else if...else

R switch

Loops

R Loops R repeat loop R while loop R for loop R break

Strings

R Strings Find length of String in R Extract Substring from a String in R Concatenate two or more Strings in R

Functions

R Functions

DataFrame

R Data Frame Sort R Data Frame by Column For each row in an R Data Frame Import Excel Data into R Dataframe Convert R Dataframe to Matrix R Dataframe - Delete Rows R Dataframe - Drop Columns R Dataframe - Add Column R Dataframe - Change Column Name R Dataframe - Remove Duplicate Rows R Dataframe - Replace NA with 0 Convert Matrix to R Dataframe

Handling Data from Files

R CSV Files - Read, Filter, Write R Read Excel XLS XLSX files

Charts & Graphs

R Pie Charts R Line Graphs

Statistical Analysis

R Mean of a Vector R Median of a Vector

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download