如何在 R 中修复:参数不是数字或逻辑:返回 na
在本文中,我们将看到如何修复 R 参数不是数字或逻辑返回 na。
这是您在 R 中可能面临的警告消息。它采用以下形式:
Warning message:
In mean.default(dataframe) : argument is not numeric or logical: returning NA
当我们尝试在 R 中计算对象的平均值但该对象包含非数字或不合逻辑的值时,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)
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 in
# section column
mean(dataframe$team)
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
# of the dataframe
mean(dataframe)
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
mean(dataframe$minor)
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(df, mean, 2)
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)
输出:

输出
现在我们将尝试计算 section 列中值的平均值。
例子:
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 in
# section column
mean(dataframe$team)
输出:

输出
正如您在输出中看到的那样,R 编译器生成“参数不是数字或逻辑:返回 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
# of the dataframe
mean(dataframe)
输出:

输出
如何避免这个警告?
避免此警告的唯一方法是将 mean()函数与仅具有数值的向量一起使用。例如,在上面的示例中,我们可以计算次要列的平均值,因为它仅包含数值:
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
mean(dataframe$minor)
输出:

输出
使用 sapply()函数的意思
R 提供了 sapply()函数来计算数据帧每一列中值的平均值。
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(df, mean, 2)
输出:

输出
正如您在输出中看到的那样,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))
# Try to calculate mean of values stored
# at the column minor in the dataframe
sapply(dataframe[c('minor', 'major')], mean, 2)
输出:

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