model.matrix Error: $ operator is invalid for atomic vectors
model.matrix Error: $ operator is invalid for atomic vectors
我运行在使用'model.matrix'时遇到这个错误。
data_A <- data.frame(X1 = c("Y","N"), X2 = c(20,24), Y = c("N","Y"))
data_A
model.matrix("Y ~ X1 + X2", data_A)
Error: $ operator is invalid for atomic vectors
导致问题的原因是什么?
检查 ?model.matrix
。一个片段:
## Default S3 method:
model.matrix(object, data = environment(object),
contrasts.arg = NULL, xlev = NULL, ...)
Arguments:
object: an object of an appropriate class. For the default method, a
model formula or a ‘terms’ object.
您的 object
是字符串公式,而 data
是 data_A
。 object
参数应该是规定的公式或术语对象。尝试
model.matrix(Y ~ X1 + X2, data_A)
或等价地(如果您从字符串构造公式)
model.matrix(as.formula(Y ~ X1 + X2), data_A)
我运行在使用'model.matrix'时遇到这个错误。
data_A <- data.frame(X1 = c("Y","N"), X2 = c(20,24), Y = c("N","Y"))
data_A
model.matrix("Y ~ X1 + X2", data_A)
Error: $ operator is invalid for atomic vectors
导致问题的原因是什么?
检查 ?model.matrix
。一个片段:
## Default S3 method:
model.matrix(object, data = environment(object),
contrasts.arg = NULL, xlev = NULL, ...)
Arguments:
object: an object of an appropriate class. For the default method, a
model formula or a ‘terms’ object.
您的 object
是字符串公式,而 data
是 data_A
。 object
参数应该是规定的公式或术语对象。尝试
model.matrix(Y ~ X1 + X2, data_A)
或等价地(如果您从字符串构造公式)
model.matrix(as.formula(Y ~ X1 + X2), data_A)