reformulate
あなたがしたいことをします。あなたは(あなたの例与えられた)既存のformula
から用語を抽出するには、このようなx+y ~z+b
reformulate(termlabels = c('x','y'), response = c('z','b'))
z ~ x + y
など、複数のreponses
で式を構築することができないことを
reformulate(termlabels = c('x','z'), response = 'y')
## y ~ x + z
またはインターセプト
reformulate(termlabels = c('x','z'), response = 'y', intercept = FALSE)
## y ~ x + z - 1
注意なし
attr(terms(RHS), 'term.labels')
## [1] "a" "b"
応答を取得するには、単純なアプローチ(1つの変数応答の場合)が少し異なります。
as.character(LHS)[2]
## [1] 'y'
combine_formula <- function(LHS, RHS){
.terms <- lapply(RHS, terms)
new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
response <- as.character(LHS)[2]
reformulate(new_terms, response)
}
combine_formula(LHS, list(RHS, RHS2))
## y ~ a + b + c
## <environment: 0x577fb908>
私はあなたにものための新たな方法を設定する式(アップデートで動作するように+
オペレータを定義することができ、文字ベクトルとして応答を指定する方が賢明だろう
combine_formula2 <- function(response, RHS, intercept = TRUE){
.terms <- lapply(RHS, terms)
new_terms <- unique(unlist(lapply(.terms, attr, which = 'term.labels')))
response <- as.character(LHS)[2]
reformulate(new_terms, response, intercept)
}
combine_formula2('y', list(RHS, RHS2))
のようなものを考えます式オブジェクト)あなたはまた、慎重に.
を使用してupdate.formula
を使用することができます
`+.formula` <- function(e1,e2){
.terms <- lapply(c(e1,e2), terms)
reformulate(unique(unlist(lapply(.terms, attr, which = 'term.labels'))))
}
RHS + RHS2
## ~a + b + c
update(~a+b, y ~ .)
## y~a+b
私はかなりのレベルの一般性を必要とせず、代わりに 'update'関数を使いやすくしたにもかかわらず、下記のmnelの答えは役に立ちました。私はもともと試していた。しかし一般的に、私は良い回答をアップアップしますが、私が実際に試してみるまで受け入れず、それらを保証することができます。多くの場合、私は自分自身でより良い答えを見つけ、時間があるときに本当に自己回答を提出すべきです。私は答えを受け入れるための私の基準にあまりにも厳しいですか? – f1r3br4nd