我想知道如何在下面的R代码中使用abline()命令,如果tl =2< code >e 29并且只有E 1101垂直行E 211E 112如果tl=1E 213/code>如果tl=1E 213/code>则绘制2垂直 lines (如果tl=1E 213/code>则为(ex )。“绿色”)?
以下是我的R代码(没有成功):
CBT <- function(g,r,n,tl){
curve(dt(x,n),-5,6,col="red")
abline(v=ifelse(tl==2,c(-2,2),2),col="green") ## HERE needs a fix??
}
## Test this:
CBT(.4,.05,20,2)发布于 2017-01-08 06:48:02
我们可以使用if/else条件
CBT <- function(g,r,n,tl){
curve(dt(x,n),-5,6,col="red")
if(tl==2){
abline(v=c(-2,2), col="green")
} else {
abline(v = 2, col="green") }
}
CBT(.4,.05,20,2)如果我们同时需要条件,即1,2和其他情况下的“tl”
CBT <- function(g,r,n,tl){
curve(dt(x,n),-5,6,col="red")
if(tl==2){
abline(v=c(-2,2), col="green")
} else if(tl==1){
abline(v = 2, col="green")
} else {abline(v=NA)}
}
CBT(.4,.05,20,2)
CBT(.4,.05,20,1)
CBT(.4,.05,20,0)发布于 2017-01-08 06:58:08
您可以使用一个ifelse调用,该调用将索引返回到具有两个条件的适当值的列表中。
CBT <- function(g,r,n,tl){
curve(dt(x,n),-5,6,col="red")
abline(v= list( c(-2,2), 2) [[ ifelse(tl==2, 1, 2)]], col="green")
}
CBT(.4,.05,20,1.5)我认为尝试以您的方式使用ifelse的问题是,如果要返回与条件长度相同的值。使用作为索引可以使您改变长度。
https://stackoverflow.com/questions/41530211
复制相似问题