从 R 编程中的对象中删除不必要的值 - na.omit()函数
R 语言中的na.omit()
函数用于从数据框、矩阵或向量中省略所有不必要的情况。
Syntax: na.omit(data)
Parameter:
data: Set of specified values of data frame, matrix or vector.
Returns: Range of values after NA omission.
示例 1:
# R program to remove
# unnecessary values
# Creating a data frame
data <- data.frame(
# Column with 2 missing values
x1 = c(1, 2, NA, 5, 9, 7, NA),
# Column with 1 missing values
x2 = c(11, 1, NA, 1, 7, 9, 1),
# Column without missing values
x3 = c(5, 7, 6, 2, 8, 1, 6)
)
data
# Apply na.omit() function
data_omit <- na.omit(data)
# Print omitted data
data_omit
输出:
x1 x2 x3
1 1 11 5
2 2 1 7
3 NA NA 6
4 5 1 2
5 9 7 8
6 7 9 1
7 NA 1 6
x1 x2 x3
1 1 11 5
2 2 1 7
4 5 1 2
5 9 7 8
6 7 9 1
示例 2:
# R program to remove
# unnecessary values
# Creating a data frame
data <- data.frame(x1 = c(1, 2, NA, 5, 9, 7, NA))
# Original data vector with NAs
data$x1
# Calling na.omit() function
na.omit(data$x1)
# Vector without NAs
as.numeric(na.omit(data$x1))
输出 :
[1] 1 2 NA 5 9 7 NA
[1] 1 2 5 9 7
attr(, "na.action")
[1] 3 7
attr(, "class")
[1] "omit"
[1] 1 2 5 9 7