I would like to write a function which takes 3 arguments and calculates the mean based on the value of the arguments. The dataframe I'm using has 3 variables out of which the mean uses one of them and it holds numeric values. When I do mean(df$v1, na.rm=TRUE)
I get a warning saying:
我想写一个函数,它接受3个参数,并根据参数的值计算平均值。我正在使用的数据帧有3个变量,其中均值使用其中一个变量并保存数值。当我的意思是(df $ v1,na.rm = TRUE)时,我收到一条警告:
Warning message:
In mean.default(df$v1, na.rm = TRUE) :
argument is not numeric or logical: returning NA
I tried to do mean(df$as.numeric(as.character(v1)), na.rm = TRUE)
我试着做法(df $ as.numeric(as.character(v1)),na.rm = TRUE)
Error in mean(df$as.numeric(as.character(v1)), na.rm = TRUE) :
attempt to apply non-function
Not sure how to deal with this.
不知道如何处理这个问题。
3 个解决方案
#1
1
If you get an warning for mean(df$v1, na.rm=TRUE)
then it probably means you have made an error in data entry that resulted in numbers getting coerced to characters and then to factors. Try:
如果你得到一个mean的警告(df $ v1,na.rm = TRUE),那么这可能意味着你在数据输入中犯了一个错误,导致数字被强制转换为字符,然后是强制因素。尝试:
str(df$v)
I predict you will find that to be a factor column. You can change it back to numeric with:
我预测你会发现这是一个因素列。您可以使用以下命令将其更改回数字:
df$v1 <- as.numeric(as.character(df$v1))
#2
0
You have to use
你必须使用
mean(as.numeric(as.character(df$v1)), na.rm = TRUE)
#3
0
The suggested way to convert factors to numeric is to use levels. From the as.factor help file:
将因子转换为数字的建议方法是使用级别。从as.factor帮助文件:
In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).
特别是,as.numeric应用于一个因子是没有意义的,并且可能通过隐式强制发生。要将因子f转换为大约其原始数值,建议使用as.numeric(levels(f))[f],并且比as.numeric(as.character(f))稍微更有效。
Example:
例:
> x=factor(10:20)
> as.numeric(levels(x))[x]
[1] 10 11 12 13 14 15 16 17 18 19 20
Note this is logically the same as:
请注意,这在逻辑上与以下相同:
> x=factor(10:20)
> as.numeric(levels(x))[as.numeric(x)]
[1] 10 11 12 13 14 15 16 17 18 19 20
As factors are integers internally.
因为因素是内部整数。
#1
1
If you get an warning for mean(df$v1, na.rm=TRUE)
then it probably means you have made an error in data entry that resulted in numbers getting coerced to characters and then to factors. Try:
如果你得到一个mean的警告(df $ v1,na.rm = TRUE),那么这可能意味着你在数据输入中犯了一个错误,导致数字被强制转换为字符,然后是强制因素。尝试:
str(df$v)
I predict you will find that to be a factor column. You can change it back to numeric with:
我预测你会发现这是一个因素列。您可以使用以下命令将其更改回数字:
df$v1 <- as.numeric(as.character(df$v1))
#2
0
You have to use
你必须使用
mean(as.numeric(as.character(df$v1)), na.rm = TRUE)
#3
0
The suggested way to convert factors to numeric is to use levels. From the as.factor help file:
将因子转换为数字的建议方法是使用级别。从as.factor帮助文件:
In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor f to approximately its original numeric values, as.numeric(levels(f))[f] is recommended and slightly more efficient than as.numeric(as.character(f)).
特别是,as.numeric应用于一个因子是没有意义的,并且可能通过隐式强制发生。要将因子f转换为大约其原始数值,建议使用as.numeric(levels(f))[f],并且比as.numeric(as.character(f))稍微更有效。
Example:
例:
> x=factor(10:20)
> as.numeric(levels(x))[x]
[1] 10 11 12 13 14 15 16 17 18 19 20
Note this is logically the same as:
请注意,这在逻辑上与以下相同:
> x=factor(10:20)
> as.numeric(levels(x))[as.numeric(x)]
[1] 10 11 12 13 14 15 16 17 18 19 20
As factors are integers internally.
因为因素是内部整数。