extract variables in formula from a data frame

本秂侑毒 提交于 2019-12-04 00:14:45

This should work:

> fr[gsub(" ","",rownames(attr(terms.formula(ff), "factors")))]
  log(Reaction) log(1+Days) x y
1             1           1 1 1
2             2           2 2 2
3             3           3 3 3
4             4           4 4 4

And props to Roman Luštrik for pointing me in the right direction.

Edit: Looks like you could pull it out off the "variables" attribute as well:

fr[gsub(" ","",attr(terms(ff),"variables")[-1])]

Edit 2: Found first problem case, involving I() or offset():

ff <- I(log(Reaction)) ~ I(log(1+Days)) + x + y
fr[gsub(" ","",attr(terms(ff),"variables")[-1])]

Those would be pretty easy to correct with regex, though. BUT, if you had situations like in the question where a variable is called, e.g., log(x) and is used in a formula alongside something like I(log(y)) for variable y, this will get really messy.

It looks to me like the only problem is the lack of a space in the name of the second column of fr. Rename it with a space and pull the columns in this way:

ff <- log(Reaction) ~ log(1+Days) + x + y
fr <- data.frame(`log(Reaction)`=1:4,
                 `log(1 + Days)`=1:4,
                 x=1:4,
                 y=1:4,
                 z=1:4,
                 check.names=FALSE)


fr[labels(terms(ff))]

If you believe the only difference between the two will always be that the names of fr has spaces where the names in ff don't, then the above solution holds. I like labels(terms(x)) a bit more, though, because it seems a bit more abstract.

fr[gsub(pattern = ' ', replacement = '', x = labels(terms(ff)))]
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!