-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonemodelframe.R
43 lines (34 loc) · 1.27 KB
/
onemodelframe.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
formulasFrame<-function(formulas,
data=parent.frame(),
na.action=getOption("na.action"),subset=NULL,
check.env=FALSE, one.frame=TRUE){
pf<-parent.frame()
## Doug wants formulas to be objects with formula methods
formulas<-lapply(formulas,formula)
if(check.env){
envs<-lapply(formulas, environment)
hasenv<-which(sapply(envs,is.null))
if (length(hasenv)>1){
for(i in 2:length(hasenv))
if (!identical(envs[[hasenv[1]]],envs[[hasenv[i]]]))
warning("Different environments on formulas")
}
}
mfs<-eval(substitute(lapply(formulas,model.frame,data=data,
na.action="na.pass",subset=subset),
list(subset=subset,formulas=formulas,data=data)),pf)
if (one.frame){
mf<-do.call("cbind",mfs)
rm(mfs)
mf<-mf[,!duplicated(names(mf)),drop=FALSE]
return(get(na.action)(mf))
} else {
naa<-lapply(mfs,function(x) attr(get(na.action)(x),"na.action"))
drop<-unique(do.call("c",naa))
class(drop)<-class(naa[[min(which(sapply(naa,is.null)))]])
if (length(drop)){
mfs<-lapply(mfs, function(x) {x<-x[-drop,,drop=FALSE]; attr(x,"na.action")<-drop; x})
}
return(mfs)
}
}