Below is a list of 5 different methods to remove rows with NA in one specific column of a data frame in R.
is.na()
Functionnewdf <- df[!is.na(df$column_name),]
subset()
Functionnewdf <- subset(df,!is.na(column_name))
complete.cases()
Functionnewdf <- df[complete.cases(df$column_name), ]
filter()
Functionlibrary(dplyr) newdf <- filter(df,!is.na(column_name))
drop_na()
Functionlibrary(tidyr) newdf <- df %>% drop_na(column_name)
Let's prepare a sample dataframe for demonstration purposes. The name of dataframe is df
. It contains two columns and 6 rows.
df <- data.frame(name = c('deeps','sandy', 'david', NA,'preet',NA), sales = c(50, 100, 45, 100, 90, NA))
is.na()
FunctionThe code below shows how to remove rows from a data frame that have missing values (NA) in a particular column using the is.na()
function.
newdf <- df[!is.na(df$name),]
name sales 1 deeps 50 2 sandy 100 3 david 45 5 preet 90
The code creates a new data frame called newdf
by selecting rows from the original data frame "df" where the values in the column named name
are not NA.
subset()
FunctionThe following code demonstrates how to remove rows from a data frame if they contain missing values (NA) in a specific column using the subset() function.
newdf <- subset(df,!is.na(name))
name sales 1 deeps 50 2 sandy 100 3 david 45 5 preet 90
We selected rows from the original data frame "df" where the values in the "name" column are not missing (NA). The is.na() function is used to identify the rows with missing values, and the negation operator (!) is applied to select the rows where the values are not NA.
complete.cases()
FunctionThe following code shows how to remove rows from a data frame with NA values in a particular column using the complete.cases() function.
newdf <- df[complete.cases(df$name), ]
name sales 1 deeps 50 2 sandy 100 3 david 45 5 preet 90
filter()
FunctionIn the code below, we are using the filter
function from the dplyr
package and is.na()
function to remove rows from a data frame with NA values in a specific column.
library(dplyr) newdf <- filter(df,!is.na(name))
name sales 1 deeps 50 2 sandy 100 3 david 45 5 preet 90
drop_na()
FunctionHere we are using the drop_na
function from the tidyr
package to remove rows from a data frame with NA values in a specific column.
library(tidyr) newdf <- df %>% drop_na(name)
name sales 1 deeps 50 2 sandy 100 3 david 45 5 preet 90
Share Share Tweet