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:
A sample dataframe named
data
is created with two columns (ID
andValue
). TheValue
column contains some missing values denoted byNA
.The original dataframe is printed to the console for reference.
The
na.omit()
function is applied to the dataframedata
to filter out rows with missing values. The result is stored in a new dataframe namedfiltered_data
.The filtered dataframe, which excludes rows with missing values, is printed to the console.