📜  如何在 R 中使用 na.rm?

📅  最后修改于: 2022-05-13 01:55:41.039000             🧑  作者: Mango

如何在 R 中使用 na.rm?

在本文中,我们将讨论如何在 R 编程语言中使用 na.rm。 R 中的 na.rm 用于删除 NA 值。

向量中的 na.rm

当我们执行任何操作时,我们必须排除 NA 值,否则结果将是 NA。

示例 1:在此示例中,我们计算平均值、总和、最小值、最大值和标准差,NA

R
# create a vector
data = c(1,2,3,NA,45,34,NA,NA,23)
  
# display
print(data)
  
# calculate mean including NA values
print(mean(data,na.rm=FALSE))
  
# calculate sum including NA values
print(sum (data,na.rm=FALSE))
  
# get minimum including NA values
print(min(data,na.rm=FALSE))
  
# get  maximum  including NA values
print(max(data,na.rm=FALSE))
  
# calculate standard deviation including
# NA values
print(sd (data,na.rm=FALSE))


R
# create a vector
data = c(1,2,3,NA,45,34,NA,NA,23)
  
# display
print(data)
  
# calculate mean excluding NA values
print(mean(data,na.rm=TRUE))
  
# calculate sum excluding NA values
print(sum (data,na.rm=TRUE))
  
# get minimum excluding  NA values
print(min(data,na.rm=TRUE))
  
# get  maximum  excluding  NA values
print(max(data,na.rm=TRUE))
  
# calculate standard deviation excluding 
# NA values
print(sd (data,na.rm=TRUE))


R
# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean including  NA values
apply(data, 2, mean , na.rm = FALSE)
  
# calculate sum including  NA values
apply(data, 2, sum , na.rm = FALSE)
  
# calculate min including  NA values
apply(data, 2, min, na.rm = FALSE)
  
# calculate max including  NA values
apply(data, 2, max , na.rm = FALSE)
  
  
# calculate standard deviation including 
# NA values
apply(data, 2, sd, na.rm = FALSE)


R
# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean excluding  NA values
apply(data, 2, mean , na.rm = TRUE)
  
# calculate sum excluding    NA values
apply(data, 2, sum , na.rm = TRUE)
  
# calculate min excluding  NA values
apply(data, 2, min, na.rm = TRUE)
  
# calculate max excluding  NA values
apply(data, 2, max , na.rm = TRUE)
  
# calculate standard deviation excluding 
# NA values
apply(data, 2, sd, na.rm = TRUE)


输出:

[1]  1  2  3 NA 45 34 NA NA 23
[1] NA
[1] NA
[1] NA
[1] NA
[1] NA

示例 2:在此示例中,我们正在计算没有 NA 的平均值、总和、最小值、最大值和标准差

R

# create a vector
data = c(1,2,3,NA,45,34,NA,NA,23)
  
# display
print(data)
  
# calculate mean excluding NA values
print(mean(data,na.rm=TRUE))
  
# calculate sum excluding NA values
print(sum (data,na.rm=TRUE))
  
# get minimum excluding  NA values
print(min(data,na.rm=TRUE))
  
# get  maximum  excluding  NA values
print(max(data,na.rm=TRUE))
  
# calculate standard deviation excluding 
# NA values
print(sd (data,na.rm=TRUE))

输出:

[1]  1  2  3 NA 45 34 NA NA 23
[1] 18
[1] 108
[1] 1
[1] 45
[1] 18.86796

数据框中的 na.rm

我们必须使用 apply函数将函数应用于具有 na.rm函数的数据帧

示例 1 :在此示例中,我们正在计算所有列中没有 NA 的平均值、总和、最小值、最大值和标准差

R

# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean including  NA values
apply(data, 2, mean , na.rm = FALSE)
  
# calculate sum including  NA values
apply(data, 2, sum , na.rm = FALSE)
  
# calculate min including  NA values
apply(data, 2, min, na.rm = FALSE)
  
# calculate max including  NA values
apply(data, 2, max , na.rm = FALSE)
  
  
# calculate standard deviation including 
# NA values
apply(data, 2, sd, na.rm = FALSE)

输出:

示例 2:排除 NA 值

R

# create a dataframe with 3 columns
data = data.frame(column1 = c(1,2,NA,34),
                  column2 = c(NA,34,56,NA),
                  column3 = c(NA,NA,32,56))
  
# display
print(data)
  
# calculate mean excluding  NA values
apply(data, 2, mean , na.rm = TRUE)
  
# calculate sum excluding    NA values
apply(data, 2, sum , na.rm = TRUE)
  
# calculate min excluding  NA values
apply(data, 2, min, na.rm = TRUE)
  
# calculate max excluding  NA values
apply(data, 2, max , na.rm = TRUE)
  
# calculate standard deviation excluding 
# NA values
apply(data, 2, sd, na.rm = TRUE)

输出: