You are on page 1of 1

Products Search… 1 1

Home
R - check if NA exists in any column of r dataframe row, then if so remove that Ask Question

PUBLIC row [duplicate]


Stack Overflow
Asked 4 years, 5 months ago Active 4 years, 5 months ago Viewed 62k times
Tags

Users The Overflow Blog


This question already has answers here:
FIND A JOB 6 Remove rows with all or some NAs (missing values) in data.frame (16 answers)
Sequencing your DNA with a USB dongle
Jobs Closed 4 years ago. and open source code

Companies Podcast 310: Fix-Server, and other useful


command line utilities
I have a large dataframe that has many rows and columns, and I would like to remove the rows for
1
TEAMS What’s this? which at least 1 column is NA / NaN. Below is a small example of the dataframe I am working with:
Featured on Meta
Free 30 Day Trial
team_id athlete_id GP tm_STL tm_TOV player_WS Opt-in alpha test for a new Stacks editor
1 13304 75047 1 2 8 NaN
2 13304 75048 1 2 8 0.28563827 Visual design changes to the review
3 13304 75049 1 2 8 NaN queues
4 13304 75050 1 2 8 NaN
5 13304 75053 1 2 8 0.03861989
Hot Meta Posts
6 13304 75060 1 2 8 -0.15530707

19 Close common misunderstanding as


...albeit a bad example because all of the NaNs show up in the last column in this case. i am duplicate?
familiar with the approach of which(is.na(df$column_name)) for getting the rows with NA values 16 Is there a canonical question for user-
from an individual column, but again want to do something like this for rows where at least 1 defined type guards in TypeScript?
column in a row of a dataframe has an NA value.
24 Is there a difference between [reactive-
forms] and [angular-reactive-forms]?
Thanks!

r Love this site?

Get the weekly newsletter! In it, you'll get:


asked Aug 12 '16 at 18:03
Share Edit Follow Canovice The week's top questions and answers
4,435 7 36 93
Important community announcements
Questions that need answers

3 na.omit or complete.cases – alistaire Aug 12 '16 at 18:06 Sign up for the digest

add a comment see an example newsletter

3 Answers Active Oldest Votes


Linked

Try using complete.cases . 915 Remove rows with all or some NAs
(missing values) in data.frame

26 > df <- data.frame(col1 = c(1, 2, 3, NA, 5), col2 = c('A', 'B', NA, 'C', 'D'), Related
col3 = c(9, NaN, 8, 7, 6))
> df
1377 How to sort a dataframe by multiple
col1 col2 col3
column(s)
1 1 A 9
2 2 B NaN 418 Check existence of directory and create if
3 3 <NA> 8 doesn't exist
4 NA C 7
5 5 D 6 915 Remove rows with all or some NAs
> df[complete.cases(df), ] (missing values) in data.frame
col1 col2 col3
1 1 A 9 37 Subset dataframe by multiple logical
conditions of rows to remove
5 5 D 6
6 Remove rows in dataframe based on three
columns
edited Aug 13 '16 at 2:18 answered Aug 12 '16 at 18:06
Share Edit Follow 797 data.table vs dplyr: can one do something
akrun Sam
well the other can't or does poorly?
634k 22 358 456 1,103 16 21
32 How to remove rows with inf from a
dataframe in R
The complete.cases should be faster than the rest – akrun Aug 13 '16 at 2:18
1 Keep rows in dataframe for the last n
add a comment appearances of a value in a column

0 Remove rows with value below zero in the


You can use this. column in each list's dataframe in R

9 Hot Network Questions


df[rowSums(is.na(df))==0,]

Is it wrong to demand features in open-source


# team_id athlete_id GP tm_STL tm_TOV player_WS
projects?
#2 13304 75048 1 2 8 0.28563827
#5 13304 75053 1 2 8 0.03861989 Is Seiryu Miharashi Station the only train station
#6 13304 75060 1 2 8 -0.15530707 where passengers cannot enter or exit the
platform?

Short story about science fiction editor in thrall to a


This way you count the number of NAs per row. You only keep the rows were the sum of non-NAs "witch doctor's" aspirations to sci-fi authorship
is zero. In PostgreSQL, how do I make it so that either
column A or column B must be non-null?

answered Aug 12 '16 at 18:11 Is it immoral to advise PhD students in non-


Share Edit Follow industry-relevant topics in middle-lower ranked
milan universities?
3,916 1 16 30
more hot questions
add a comment

na.omit works:

9 na.omit(df)
## team_id athlete_id GP tm_STL tm_TOV player_WS
## 2 13304 75048 1 2 8 0.28563827
## 5 13304 75053 1 2 8 0.03861989
## 6 13304 75060 1 2 8 -0.15530707

It's a little more convenient than complete.cases if you're piping, as it doesn't require another
function to subset like dplyr::filter , magrittr::extract , or [ .

edited Aug 12 '16 at 18:13 answered Aug 12 '16 at 18:08


Share Edit Follow alistaire
38k 4 57 89

add a comment

Not the answer you're looking for? Browse other questions tagged r or ask your own question.

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
site design / logo © 2021 Stack Exchange Inc; user contributions
Disable Responsiveness Contact Us licensed under cc by-sa. rev 2021.2.4.38498

You might also like