R";密度错误。默认值(原始):参数';x';必须是数字“;在data.frame中使用所有int值
当我的所有类型都是数字时,为什么会出现这个错误R";密度错误。默认值(原始):参数';x';必须是数字“;在data.frame中使用所有int值,r,dataframe,types,R,Dataframe,Types,当我的所有类型都是数字时,为什么会出现这个错误 > str(raw) 'data.frame': 404166147 obs. of 8 variables: $ piece_1_A : int 0 0 0 0 0 0 0 0 0 0 ... $ piece_1_B : int 0 0 0 0 0 0 0 0 0 0 ... $ piece_2_A : int 0 0 0 0 0 0 0 0 0 0 ... $ piece_2_B : int 0 0 0 0 0
> str(raw)
'data.frame': 404166147 obs. of 8 variables:
$ piece_1_A : int 0 0 0 0 0 0 0 0 0 0 ...
$ piece_1_B : int 0 0 0 0 0 0 0 0 0 0 ...
$ piece_2_A : int 0 0 0 0 0 0 0 0 0 0 ...
$ piece_2_B : int 0 0 0 0 0 0 0 0 0 0 ...
$ item_1_A : int 0 0 0 0 0 0 0 0 0 0 ...
$ item_1_B : int 0 0 0 0 0 0 0 0 0 0 ...
$ piece_2_A: int 0 0 0 0 0 0 0 0 0 0 ...
$ piece_2_B: int 0 0 0 0 0 0 0 0 0 0 ...
> sapply(raw, typeof)
item_1_A item_1_B item_2_A item_2_B piece_1_A piece_1_B piece_2_A
"integer" "integer" "integer" "integer" "integer" "integer" "integer"
piece_2_B
"integer"
> density(raw[,1])
Error in density.default(raw[, 1]) : argument 'x' must be numeric
我建议检查变量的类。第一列可以是因子或字符,
typeof
仍将返回整数。您需要将变量转换为类numeric
要查看typeof
和class
的行为,请尝试以下示例:
# load sample dataset
data(mtcars)
# check class and type of all variables
sapply(mtcars, class)
sapply(mtcars, typeof)
# convert gear variable to class factor
mtcars$gear <- as.factor(mtcars$gear)
# check class and type again
sapply(mtcars, class)
sapply(mtcars, typeof)
#加载样本数据集
数据(mtcars)
#检查所有变量的类和类型
赛普利(mtcars,等级)
sapply(mtcars,类型)
#将档位变量转换为等级系数
mtcars$gear