简体   繁体   English

model.matrix错误:$运算符对原子向量无效

[英]model.matrix Error: $ operator is invalid for atomic vectors

I ran into this error when using 'model.matrix'. 使用'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

What's causing the problem? 是什么原因引起的?

Examine ?model.matrix . 检查?model.matrix A snippet: 摘录:

     ## 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.

Your object is a string formula while data is data_A . 您的object是一个字符串公式,而datadata_A The object argument should be a formula or terms object as stated. object参数应为所述的公式或术语对象。 Try 尝试

model.matrix(Y ~ X1 + X2, data_A)

or equivalently (if you are constructing the formula from a string) 或等效地(如果您是根据字符串构造公式)

model.matrix(as.formula(Y ~ X1 + X2), data_A)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM