Possible Duplicate:
R - remove rows with NAs in data.frame
How can I quickly remove "rows" in a dataframe with a NA value in one of the columns?
So
x1 x2 [1,] 1 100 [2,] 2 NA [3,] 3 300 [4,] NA 400 [5,] 5 500
should result in:
x1 x2 [1,] 1 100 [3,] 3 300 [5,] 5 500
To remove all rows having NA, we can use na. omit function. For Example, if we have a data frame called df that contains some NA values then we can remove all rows that contains at least one NA by using the command na. omit(df).
Select "Blanks" and click OK. Excel has now selected all of the blank cells in the column. Now carefully right-mouse click on one of the empty cells, and choose Delete from the menu. Then select Entire row, and click the OK button.
To drop rows with NA's in some specific columns, you can use the filter() function from the dplyr package and the in.na() function. First, the latter one determines if a value in a column is missing and returns a TRUE or FALSE. Next, the filter function drops all rows with an NA.
na. omit will omit all rows from the calculations.
dat <- data.frame(x1 = c(1,2,3, NA, 5), x2 = c(100, NA, 300, 400, 500)) na.omit(dat) x1 x2 1 1 100 3 3 300 5 5 500
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With