Error in Math.factor(x) : ‘abs’ not meaningful for factors

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Error in Math.factor(x) : ‘abs’ not meaningful for factors相关的知识,希望对你有一定的参考价值。

Error in Math.factor(x) : ‘abs’ not meaningful for factors

Error in Math.factor(x_fac): \'abs\' not meaningful for factors
Traceback:

1. Math.factor(x_fac)
2. stop(gettextf("%s not meaningful for factors", sQuote(.Generic)))

问题:

factor变量使用abs函数会报错哦,,,

x <- c(- 5, 9, 3, - 1, 2)                 # Create example vector
# -5  9  3 -1  2


x_fac <- factor(x)                        # Convert example vector to factor
x_fac                                     # Print output to RStudio console
# [1] -5 9  3  -1 2 
# Levels: -5 -1 2 3 9
abs(x_fac)                                # Apply abs function to factor
# Error in Math.factor(x_fac) : \'abs\' not meaningful for factors

解决:

# 把factor转化为数值型变量才可以

x_num <- as.numeric(as.character(x_fac))  # Convert factor to numeric
x_num_abs <- abs(x_num)

以上是关于Error in Math.factor(x) : ‘abs’ not meaningful for factors的主要内容,如果未能解决你的问题,请参考以下文章

# Error in colSums(iris) : ‘x‘ must be numeric,# Error in rowSums(iris) : ‘x‘ must be numeric

Error in exists(x): 第一个参数不对

Error: ‘X‘ is an unrecognized escape in character string starting

Error in hist.default(data) : ‘x‘ must be numeric

R语言学习笔记-Error in ts(x):对象不是矩阵问题解决

Error in ggplot(df, aes(x = x, y = y)) : could not find function “ggplot“