开发者

model.matrix generates fewer rows than original data.frame

开发者 https://www.devze.com 2023-03-14 15:41 出处:网络
Why doesn\'t a model matrix necessarily have the same number of rows as the data frame? mergem = model.matrix(as.formula(paste(response, \'~ .\')), data=mer开发者_如何学运维gef)

Why doesn't a model matrix necessarily have the same number of rows as the data frame?

mergem = model.matrix(as.formula(paste(response, '~ .')), data=mer开发者_如何学运维gef)
dim(mergef)
# [1] 115562     71
dim(mergem)
# [1] 66786   973

I tried looking for hints in the documentation but couldn't find anything. Thanks in advance.


Well, if a row has NAs in it, that row is (by default) removed:

d <- data.frame(x=c(1,1,2), y=c(2,2,4), z=c(4,NA,8))
m <- model.matrix(x ~ ., data=d)

nrow(d) # 3
nrow(m) # 2

This behavior is controlled by the option "na.action":

options(na.action="na.fail")
m <- model.matrix(x ~ ., data=d) # Error: missing values in object
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号