R语言如何修复:Argument is not numeric or logical: returning na

R语言如何修复:Argument is not numeric or logical: returning na

在这篇文章中,我们将看到如何在R语言中修复参数不是数字或逻辑的情况下返回na。

这是你在R中可能面临的警告信息,其形式如下。

警告信息。

In mean.default(dataframe) : argument is not numeric or logical: returning NA

当我们试图在R中计算一个对象的平均值,但该对象包含非数字或不符合逻辑的值时,R编译器会产生这种类型的错误。本文重点介绍了我们如何解决这个错误。

什么时候可能发生这种错误

让我们先创建一个数据框。

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh', 'Anil',
                                   'Suraj', 'Piyush', 
                                   'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Print the dataframe
print(dataframe)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

输出

现在我们将尝试计算章节栏中数值的平均值。

例子

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh',
                                   'Anil', 'Suraj', 
                                   'Piyush', 'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Try to calculate mean of values in 
# section column
mean(dataframe$team)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

输出

正如你在输出中看到的,R编译器产生了 “参数不是数字或逻辑:返回NA”,因为该部分有非数字值。

现在我们来计算一下数据框的平均值。

例子

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh',
                                   'Anil', 'Suraj',
                                   'Piyush', 'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Try to calculate mean of values 
# of the dataframe
mean(dataframe)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

输出

如何避免这个警告

避免这个警告的唯一方法是对只有数字值的向量使用mean()函数。例如,在上面的例子中,我们可以计算小列的平均值,因为它只包含数值。

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh', 'Anil', 
                                   'Suraj', 'Piyush', 
                                   'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Try to calculate mean of values stored 
# at the column minor in the dataframe
mean(dataframe$minor)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

输出

使用sapply()函数的平均数

R提供了sapply()函数来计算数据框架中每一列数值的平均值。

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh', 'Anil',
                                   'Suraj', 'Piyush', 
                                   'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Try to calculate mean of values stored 
# at the column minor in the dataframe
sapply(df, mean, 2)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

输出

正如你在输出中看到的,R编译器产生了警告信息,但也计算出了其中有数字值的列的平均值。我们可以通过明确指定有数字值的列来完全避免警告。

# Create a data frame
dataframe <- data.frame(students=c('Bhuwanesh', 'Anil', 
                                   'Suraj', 'Piyush', 
                                   'Dheeraj'),
                 section=c('A', 'A', 'C', 'C', 'B'),
                 minor=c(87, 98, 71, 89, 82),
                 major=c(80, 88, 84, 74, 70))
  
# Try to calculate mean of values stored 
# at the column minor in the dataframe
sapply(dataframe[c('minor', 'major')], mean, 2)
Bash

输出

R语言如何修复:Argument is not numeric or logical: returning na

这一次,程序编译成功,没有任何警告信息。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册