[英]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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.