R Dataframe - Remove NA Rows - Examples - Tutorial Kart
[Pages:5]R Dataframe ? Remove NA Rows
In this tutorial, we will learn hot to remove rows in a dataframe with one or more NAs as column values.
Remove rows of R Dataframe with one or more NAs
To remove rows of a dataframe with one or more NAs, use complete.cases() function as shown below
resultDF = myDat aframe[c omplet e .c ases(myDat aframe),] resultDF = myDataframe[complete.cases(myDataframe),]
where
myDataframe is the dataframe containing rows with one or more NAs
resultDF is the resulting dataframe with rows not containing atleast one NA
Example ? Remove rows with NA in Dataframe
In this example, we will create a dataframe with some of the rows containing NAs.
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) > DF1 >xDFy1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) 1 9 4 2>NDAF1NA 3 7x yNA 4 4 21 1 9 4 2 NA NA 3 7 NA 4 4 21
In the second row we have all the column values as NA. In the third row, we have some columns with NA and some with numbers.
Now, we will use complete.cases() function to remove these rows in dataframe containing NAs
> resultDF = DF1[c omplet e.c ases(D F1), ] > resultDF
x y 19 4 4 4 21
> resultDF = DF1[complete.cases(DF1), ] > resultDF
x y 1 9 4 4 4 21
The resultDF contains rows with none of the values being NA.
Remove rows of R Dataframe with all NAs
In the previous example with complete.cases() function, we considered the rows without any missing values. But in this example, we will consider rows with NAs but not all NAs.
To remove rows of a dataframe that has all NAs, use dataframe subsetting as shown below
resultDF = mydataframe[rowSums (is.na(mydataframe[ , 0:nc ol(mydat aframe)]) )re DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) > DF1 >xDFy1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) 1 9 4 2>NDAF1NA 3 7x yNA 4 4 21 1 9 4 2 NA NA 3 7 NA 4 4 21
In the second row we have all the column values as NA. In the third row, we have some columns with NA and some with numbers.
Now, we will use dataframe subsetting to remove these rows in dataframe containing all NAs.
> resultDF = DF1[rowSums(is.na(DF 1[ , 0:ncol(DF1)])) < ncol(DF1), ] >>rreessuultltDDFF = DF1[rowSums(is.na(DF1[ , 0:ncol(DF1)])) < ncol(DF1), ]
x y 1>9res4ultDF 3 7x yNA 4 4 21 >1 9 4 3 7 NA 4 4 21 >
The resultDF contains rows with none of the rows having all NAs.
Example ? Remove rows with all NAs in Dataframe
In this example, we will create a dataframe with some of the rows containing NAs.
> DF1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) > DF1 >xDFy1 = data.frame(x = c(9, NA, 7, 4), y = c(4, NA, NA, 21)) 1 9 4 2>NDAF1NA 3 7x yNA 4 4 21 1 9 4 2 NA NA 3 7 NA 4 4 21
In the second row we have all the column values as NA. In the third row, we have some columns with NA and some with numbers.
Now, we will use dataframe subsetting to remove these rows in dataframe containing all NAs.
> resultDF = DF1[rowSums(is.na(DF 1[ , 0:ncol(DF1)])) < ncol(DF1), ] > resultDF
x y 19 4 3 7 NA 4 4 21 >
> resultDF = DF1[rowSums(is.na(DF1[ , 0:ncol(DF1)])) < ncol(DF1), ] > resultDF
x y 1 9 4 3 7 NA 4 4 21 >
The resultDF contains rows with none of the rows having all NAs.
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
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.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- assignments q 1 write statement to remove index for given dataframe as
- cheat sheet pandas python datacamp
- pandas datareader documentation read the docs
- 3 how to remove column from dataframe using del statement
- 56 proc of the 9th python in science conf scipy 2010 data
- data wrangling tidy data pandas
- data handling using pandas 1
- pandasguide read the docs
- pandas dataframe notes university of idaho
- r dataframe remove na rows examples tutorial kart
Related searches
- remove null rows python
- powershell remove blank rows csv
- r dataframe select multiple columns
- r dataframe size
- r dataframe column name
- r dataframe get column
- r dataframe select column
- r dataframe column types
- r dataframe change column type
- how to remove blank rows in excel
- excel remove blank rows formula
- dataframe find unique rows only in one