基于R中输入参数的mtext()的选择性文本? [英] Selective text for mtext() based on the input argument in R?
问题描述
我有一个向量化的R函数可以产生一些直方图(如下)。我想为其中一个直方图添加一个 mtext()
(请在下面找到它)。
问题
但是我希望 mtext()
中的文本根据参数 n
是长度> 1的向量或者参数 es
是长度大于1的向量。
txt = if(length(n)> 1)在我的代码中, Group Sample Size =else if(length(es)> 1)Effect Size =elseGroup Sample Size =
mtext(paste0(txt,input),3)
[请在下面找到这些]
是我完整的R代码:
p.es = function(n,es,n.sim){$如果(长度(n)> 1)n else if(length(es)> 1)其他n
t.sim = Vectorize(函数( n,es){
d = numeric(n.sim)
p = numeric(n.sim)
for(i in 1:n.sim){
N = sqrt((n ^ 2)/(2 * n) )
x = rnorm(n,es,1)
y = rnorm(n,0,1)
a = t.test(x,y,var.equal = TRUE)
d [i] = a [[1]] / N
p [i] = a [[3]]}
txt = if(length(n)> 1)Group Sample Size =else if(length(es)> 1)Effect Size =elseGroup Sample Size =
hist(p); mtext(paste0(txt,input),3)##这里我需要帮助!!!!
hist(d)
},c(n,es))
par(mfcol = c(2,length(input)) ,xpd = NA)
不可见(t.sim(n,es))}
#使用示例:
p.es(n = 20,es = c(.1,。 2),n.sim = 20)#这里我期望多行文字显示Effect Size =
#但它不是为什么?
我不认为问题出在 mtext
您的 if ... else
语句。它看起来问题是由于 Vectorize
而发生的。让我们从你的内部函数开始,这里叫做 myfun
。
myfun< - function(n,es,nsim,input){
pre>
d = numeric(n.sim)
p = numeric(n.sim)
for(i in 1:n.sim){
N = sqrt((n ^ 2)/(2 * n))
x = rnorm(n,es,1)
y = rnorm(n,0,1)
a = t.test(x,y,var .equal = TRUE)
d [i] = a [[1]] / N
p [i] = a [[3]]}
txt < - if长度(n)> 1){组样本大小=} else if(长度> 1){效果大小=} else {组样本大小=}
hist(p ); mtext(paste0(txt,input,collapse =),3)##这里我需要帮助!!!!
hist(d)
}
#测试运行
par(mfrow = c(1,2))
myfun(n = 10,
es = c(0.1,0.2),
nsim = 10,
input = c(0.1,0.2))
现在,如果您实现
Vectorize
,问题就出现了。
t.sim< - Vectorize(myfun ,c('n','es','nsim','input'))
n < - 10
n.sim < - 10
es < - c(0.1 ,0.2)
input< -c(0.1,0.2)
t.sim(n,es,n.sim,input)
解决这个问题的方法之一是移动
txt
在Vectorize
之外的任务调用当你定义input
?)时,你能分配txt
的值吗?p.es = function(n,es,n.sim){
input = if(length(n)> 1)其他如果(长度> 1)其他n
txt = if(length(n)> 1)Group Sample Size =else if(length(1)> 1) Effect Size =elseGroup Sample Size =
t.sim = Vectorize(function(n,es){
d = numeric(n.sim)
p = numeric (n.sim)
(i in 1:n.sim){
N = sqrt((n ^ 2)/(2 * n))
x = rnorm (n,es,1)
y = rnorm(n,0,1)
a = t.test(x,y,var.equal = TRUE)
d [i] = a [[ 1]] / N
p [i] = a [[3]]}
hist(p); mtext(paste0(txt,paste(input,collapse = - )) ,3)##这里我需要帮助!!!!
hist(d)
},c(n,es))
par(mfcol = c(2,length(input)),xpd = NA)
不可见(t.sim(n,es))}
#使用示例:
p.es (n = 20,es = c(.1,.2),n.sim = 20)#这是你想要的吗?
I have a vectorized R function that produces some histograms (below). I want to add an
mtext()
to one of the histograms (please locate this below).Question
But I want the text in the
mtext()
to change according to whether argumentn
is a vector of length > 1 OR argumentes
is a vector of length > 1.In my code, I'm using the following piece without success:
txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = "
mtext(paste0(txt, input), 3)
[Please locate these below]Here is my complete R code:
p.es = function(n, es, n.sim){ input = if(length(n) > 1) n else if(length(es) > 1) es else n t.sim = Vectorize(function(n, es){ d = numeric(n.sim) p = numeric(n.sim) for(i in 1:n.sim){ N = sqrt((n^2)/(2*n)) x = rnorm(n, es, 1) y = rnorm(n, 0, 1) a = t.test(x, y, var.equal = TRUE) d[i] = a[[1]]/N p[i] = a[[3]] } txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = " hist(p) ; mtext(paste0(txt, input), 3) ## Here I need help!!!! hist(d) }, c("n", "es")) par(mfcol = c(2, length(input)), xpd = NA) invisible(t.sim(n, es)) } # Example of use: p.es(n = 20, es = c(.1, .2), n.sim = 20) # Here I expect the mtext to show "Effect Size = " # but it doesn't why?
解决方案I don't think the problem is with
mtext
of yourif...else
statements. It looks the problem happens because ofVectorize
. Let's start with your inner function, here calledmyfun
. It seems to work nicely.myfun <- function(n, es, nsim, input){ d = numeric(n.sim) p = numeric(n.sim) for(i in 1:n.sim){ N = sqrt((n^2)/(2*n)) x = rnorm(n, es, 1) y = rnorm(n, 0, 1) a = t.test(x, y, var.equal = TRUE) d[i] = a[[1]]/N p[i] = a[[3]] } txt <- if (length(n) > 1) {"Group Sample Size = "} else if (length(es) > 1) {"Effect Size = "} else {"Group Sample Size = "} hist(p) ; mtext(paste0(txt, input, collapse = " "), 3) ## Here I need help!!!! hist(d) } # Test Run par(mfrow = c(1,2)) myfun(n = 10, es = c(0.1, 0.2), nsim = 10, input = c(0.1, 0.2))
Now, if you implement
Vectorize
, the problem appears.t.sim <- Vectorize(myfun, c('n', 'es', 'nsim', 'input')) n <- 10 n.sim <- 10 es <- c(0.1, 0.2) input <- c(0.1, 0.2) t.sim(n, es, n.sim, input)
One way to fix this is to move the
txt
assignment outside theVectorize
call (can you assign the value oftxt
when you defineinput
?).p.es = function(n, es, n.sim){ input = if(length(n) > 1) n else if(length(es) > 1) es else n txt = if(length(n) > 1) "Group Sample Size = " else if(length(es) > 1) "Effect Size = " else "Group Sample Size = " t.sim = Vectorize(function(n, es){ d = numeric(n.sim) p = numeric(n.sim) for(i in 1:n.sim){ N = sqrt((n^2)/(2*n)) x = rnorm(n, es, 1) y = rnorm(n, 0, 1) a = t.test(x, y, var.equal = TRUE) d[i] = a[[1]]/N p[i] = a[[3]] } hist(p) ; mtext(paste0(txt, paste(input, collapse = "-")), 3) ## Here I need help!!!! hist(d) }, c("n", "es")) par(mfcol = c(2, length(input)), xpd = NA) invisible(t.sim(n, es)) } # Example of use: p.es(n = 20, es = c(.1, .2), n.sim = 20) # Is this what you want?
这篇关于基于R中输入参数的mtext()的选择性文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!