Remove Missing Values in R | na.omit() Guide
In R, you can use the na.omit() function to remove rows containing missing values. This function will return a data frame with the missing rows removed.
Here is an example:
# 创建一个包含缺失值的数据框
df <- data.frame(
x = c(1, 2, NA, 4, 5),
y = c(NA, 2, 3, NA, 5)
)
# 删除包含缺失值的行
new_df <- na.omit(df)
# 输出删除缺失值后的数据框
print(new_df)
Output result:
x y
2 2 2
In the above example, the original data frame df contains two rows of data (row 1 and row 3) with missing values. By using the na.omit() function to remove these rows with missing values, a new data frame new_df is obtained with only the rows that do not contain missing values.