filter na in r

# Step 1: Create a sample dataframe with missing values
set.seed(123)
data <- data.frame(
  ID = 1:10,
  Value = c(5, 3, NA, 8, 2, NA, 10, 7, NA, 4)
)

# Step 2: Display the original dataframe
print("Original Dataframe:")
print(data)

# Step 3: Use the 'na.omit()' function to filter out rows with missing values
filtered_data <- na.omit(data)

# Step 4: Display the filtered dataframe
print("Filtered Dataframe (without NA values):")
print(filtered_data)

In this code snippet:

  1. A sample dataframe named data is created with two columns (ID and Value). The Value column contains some missing values denoted by NA.

  2. The original dataframe is printed to the console for reference.

  3. The na.omit() function is applied to the dataframe data to filter out rows with missing values. The result is stored in a new dataframe named filtered_data.

  4. The filtered dataframe, which excludes rows with missing values, is printed to the console.