我正在尝试运行一个循环,将结果存储在一个向量中。但我还需要在预定的向量上增加计数器,以使存储的计算正确运行。我被两部分卡住了:(1)增加计数器,(2)将循环的结果存储在向量中。
我刚开始接触循环,所以请容忍下面最有可能不正确的语法;以下是我正在使用的语法:
x <- c(.01,.05,.10,.20,.25) # observed defect rates
for(i in x) {
j <- 1
if(x < 1){
atmost2[] <- dbinom(0,size=10,prob=x[[j]])+
dbinom(1,size=10,prob=x[[j]])+
dbinom(2,size=10,prob=x[[j]]) &&
j <- j + 1
}
}
atmost2本质上,我希望将结果存储在一个新的向量atmost2中,每个连续的循环通过增加j来遍历x中的向量值;j应该增加,以改变dbinom中的prob参数,而不是x中的预定向量值。
有人能帮上忙吗?
发布于 2020-09-17 02:32:49
以下是一些事情:
juljo在循环之前初始化向量是正确的,他们还做了一些其他的修正,但我认为他们的代码只有在你已经建立了:
j <- 1否则,juljo的代码就会崩溃。
此外,您的代码不需要“&&”即可工作。只需将j<-j+1放在新行上,就像这样(使用julgo的代码)
j <- 1
x <- c(.01,.05,.10,.20,.25) # observed defect rates
atmost2 <- as.numeric(1:length(x)) # this initializes the vector to a predetermined length which may help with very large loops
for(i in 1:length(x)) {
if(x < 1){
atmost2[i] <- dbinom(0,size=10,prob=x[j])+ # note that the double brackets are gone
dbinom(1,size=10,prob=x[j])+
dbinom(2,size=10,prob=x[j])
}
j <- j + 1 # I think you want j to increment outside the if statement
}
atmost2这段代码做了一些事情,但是有一些警告,我不确定你想要做什么。
您还可以跳过添加dbinom的步骤,而改为如下所示:
j <- 1
x <- c(.01,.05,.10,.20,.25) # observed defect rates
atmost2 <- as.numeric(1:length(x)) # this initializes the vector to a predetermined length which may help with very large loops
for(i in 1:length(x)) {
if(x < 1){
atmost2[i] <- sum(dbinom( 0:2 , size=10,prob=x[j])) #dbinom will give a vector that can be summed
}
j <- j + 1 # I think you want j to increment outside the if statement
}
atmost2但我认为使用j迭代器可能是其他编程语言的习惯。注意,使用循环但不使用j的输出是相同的:
x <- c(.01,.05,.10,.20,.25) # observed defect rates
atmost2 <- as.numeric(1:length(x)) # this initializes the vector to a predetermined length which may help with very large loops
for(i in 1:length(x)) {
if(x < 1){
atmost2[i] <- sum(dbinom(0:2,size=10,prob=x[i]))
}
}
atmost2所有这些都会产生相同的输出:
> atmost2
[1] 0.9998862 0.9884964 0.9298092 0.6777995 0.5255928但我有一些后续问题:
atmost2的长度应该与x相同吗?
您是否使用x中的值作为概率?因此,atmost2是基于xi的值的dbinom概率的总和
它必须是一个循环吗?R很好地使用了向量,因此应用函数可能会有所帮助。您可能会发现lapply在这里很有用。?apply可能会让您开始使用,而?lapply将给出其他apply函数的说明。
因此,您的代码可能如下所示
x <- c(.01, .05, .10, .20, .25)
atmost2 <- as.numeric(1:length(x))
atmost2 <- lapply(x, function(x) sum(dbinom( 0:2 , size = 10, prob = x)))
atmost2 # this is a list, not a vectorlapply函数的代码如下:
应用于列表中的项,'x',函数。
在本例中,该函数是一个匿名函数"sum(dbinom....)“
因此,将函数sum(dbinom...)应用于x的每个值。并返回一个列表。
基本上,它会为你做循环。而且通常比for循环快几倍(在R中)。
如果你需要atmost2不是一个列表而是一个向量,你可以:
unlist(atmost2)
> unlist(atmost2)
[1] 0.9998862 0.9884964 0.9298092 0.6777995 0.5255928根据Rui的提醒进行编辑
使用sapply,其他所有东西都是一样的,但输出确实是一个向量。
x <- c(.01, .05, .10, .20, .25)
atmost2 <- as.numeric(1:length(x))
atmost2 <- sapply(x, function(x) sum(dbinom( 0:2 , size = 10, prob = x)))
atmost2 # this is a vector发布于 2020-09-17 00:58:29
像这样调用元素怎么样:
x <- c(.01,.05,.10,.20,.25) # observed defect rates
atmost2 <- numeric() # Initialize vector before filling it in the loop
for(i in 1:length(x)) {
if(x[i] < 1){
atmost2[i] <- dbinom(0,size=10,prob=x[i])+
dbinom(1,size=10,prob=x[i])+
dbinom(2,size=10,prob=x[i])
}
}
atmost2https://stackoverflow.com/questions/63924608
复制相似问题