Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建表达式测试以查看R中的'as.formula(';X~1';)`是否包含截取?_R - Fatal编程技术网

创建表达式测试以查看R中的'as.formula(';X~1';)`是否包含截取?

创建表达式测试以查看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

在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'中返回
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