Remove Missing Values in R: na.omit() Guide
In R language, you can use the na.omit() function to remove missing values from a data frame or vector. This function will return a new data frame or vector with the rows or elements containing missing values removed. For example:
# 创建一个包含缺失值的数据框
df <- data.frame(x = c(1, 2, NA, 4, 5), y = c("a", "b", NA, "d", "e"))
# 删除包含缺失值的行
df_clean <- na.omit(df)
print(df_clean)
If you want to directly remove missing values on the original data frame or vector, you can use the inplace = TRUE parameter in the na.omit() function, as shown below:
# 删除原始数据框中的缺失值
na.omit(df, inplace = TRUE)