创建表达式测试以查看R中的'as.formula(';X~1';)`是否包含截取?
在R中,可以指定一个公式:创建表达式测试以查看R中的'as.formula(';X~1';)`是否包含截取?,r,R,在R中,可以指定一个公式: F <- as.formula('X ~ 1') 还有。谢谢 我们可以提取并检查 F[[3]] == 1 因为如果我们将as.list,第三个list元素是1 as.list(F) #[[1]] # `~` #[[2]] #X #[[3]] #[1] 1 它将在OP的post中的所有3个'F'中返回TRUEattr(术语(x~y),'intercept')将执行您想要的操作 formula <- x~y formula2 <- x~y-1
F <- as.formula('X ~ 1')
还有。谢谢 我们可以提取并检查
F[[3]] == 1
因为如果我们将as.list
,第三个list
元素是1
as.list(F)
#[[1]]
# `~`
#[[2]]
#X
#[[3]]
#[1] 1
它将在OP的post中的所有3个'F'中返回TRUE
attr(术语(x~y),'intercept')
将执行您想要的操作
formula <- x~y
formula2 <- x~y-1 # no intercept
attr(terms(formula), 'intercept')
## [1] 1
attr(terms(formula2), 'intercept')
## [1] 0
formula您可以使用lazyeval
软件包:
> F <- as.formula('X ~ 1')
> lazyeval::f_rhs(F)
[1] 1
>F懒洋洋::F_rhs(F)
[1] 1
你想用~x+0
怎么办?~x+y+0
或者y~x+0
怎么样?
f1 <- x ~ y
f2 <- x ~ y-1
f3 <- x ~ 1
f3 <- x ~ 0
onlyIntercept <- function(f){
return(attr(terms(f), 'intercept') & length(attr(terms(f), 'factors')) == 0)
}
# Examples on above, then on OPs examples:
onlyIntercept(f1)
## [1] FALSE
onlyIntercept(f2)
## [1] FALSE
onlyIntercept(f3)
## [1] TRUE
onlyIntercept(f4)
## [1] FALSE
onlyIntercept(as.formula('X~ 1'))
## [1] TRUE
onlyIntercept(as.formula('X~1'))
## [1] TRUE
onlyIntercept(as.formula('X ~1'))
## [1] TRUE
> F <- as.formula('X ~ 1')
> lazyeval::f_rhs(F)
[1] 1