filter out a row which contains na in a specific column in r

To filter out a row that contains NA in a specific column in R, you can use the !is.na() function within the dplyr package or base R. Here's how you can do it:

main.r
# Load the dplyr library
library(dplyr)

# Create a sample data frame
df <- data.frame(
  id = c(1, 2, 3, 4, 5),
  name = c("Alice", "Bob", NA, "Dave", "Emily"),
  age = c(25, 30, 35, NA, 40)
)

# Filter out rows with NA in the 'name' column
filtered_df <- df[!is.na(df$name), ]

# Alternatively, using dplyr
filtered_df_dplyr <- df %>% 
  filter(!is.na(name))

# Print the results
print(filtered_df)
print(filtered_df_dplyr)
426 chars
21 lines

In this code:

  • df[!is.na(df$name), ] uses base R to select all rows where the 'name' column is not NA.
  • df %>% filter(!is.na(name)) uses the dplyr package to achieve the same result.

Both methods will produce the same output, which is a data frame with the rows containing NA in the 'name' column filtered out.

related categories

gistlibby LogSnag