将嵌套循环的结果写入 R 中的另一个向量
我对 R 还很陌生,并且在使用它方面遇到了一些困难。我有以下代码:
repeat {
if (t > 1000)
break
else {
y1 <- rpois(50, 15)
y2 <- rpois(50, 15)
y <- c(y1, y2)
p_0y <- matrix(nrow = max(y) - min(y), ncol = 1)
i = min(y)
while (i <= max(y)) {
p_0y[i - min(y), ] = (length(which(y1 == i))/50)
i <- i + 1
}
p_y <- matrix(nrow = max(y) - min(y), ncol = 1)
j = min(y)
while (j <= max(y)) {
p_y[j - min(y), ] = (length(which(y == j))/100)
j <- j + 1
}
p_0yx <- p_0y[rowSums(p_0y == 0) == 0]
p_yx <- p_y[rowSums(p_0y == 0) == 0]
g = 0
logvect <- matrix(nrow = (length(p_yx)), ncol = 1)
while (g <= (length(p_yx))) {
logvect[g, ] = (p_0yx[g])/(p_yx[g])
g <- g + 1
}
p_0yx %*% (log2(logvect))
print(p_0yx %*% (log2(logvect)))
t <- t + 1
}
}
我对最后一行之前的所有内容都很满意,但我不想将 p_0yx%*%(log2(logvect)) 的值打印到屏幕上,而是想将其存储为另一个向量。有什么想法吗?我尝试过以与嵌套循环类似的方式进行操作,但似乎不起作用。
谢谢
I'm pretty new to R, and am struggling a bit with it. I have the following code:
repeat {
if (t > 1000)
break
else {
y1 <- rpois(50, 15)
y2 <- rpois(50, 15)
y <- c(y1, y2)
p_0y <- matrix(nrow = max(y) - min(y), ncol = 1)
i = min(y)
while (i <= max(y)) {
p_0y[i - min(y), ] = (length(which(y1 == i))/50)
i <- i + 1
}
p_y <- matrix(nrow = max(y) - min(y), ncol = 1)
j = min(y)
while (j <= max(y)) {
p_y[j - min(y), ] = (length(which(y == j))/100)
j <- j + 1
}
p_0yx <- p_0y[rowSums(p_0y == 0) == 0]
p_yx <- p_y[rowSums(p_0y == 0) == 0]
g = 0
logvect <- matrix(nrow = (length(p_yx)), ncol = 1)
while (g <= (length(p_yx))) {
logvect[g, ] = (p_0yx[g])/(p_yx[g])
g <- g + 1
}
p_0yx %*% (log2(logvect))
print(p_0yx %*% (log2(logvect)))
t <- t + 1
}
}
i am happy with everything up to the last line, but instead of printing the value of p_0yx%*%(log2(logvect)) to the screen i would like to store this as another vector. any ideas? i have tried doing it a similar way as in the nested loop but doesnt seem to work.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的答案是首先声明一个变量。将其放在您在此发布的所有内容之前。我将其称为临时。它将保存所有值。
然后,
您的代码正在做一些奇怪的事情,而不是使用打印行。查看 p_0y 的第一个索引。它实际上是该矩阵中项目
0,
的索引。 R 从 1 开始索引。当您在该矩阵中创建行数时,您可以使用 max(y) - min(y)。如果最大值为 10,最小值为 1,则只有 9 行。我打赌你真的想添加一个。另外,您的代码与 R 非常不一样,带有所有不必要的 while 循环。例如,您的整个最后一个循环(以及 logvect 的初始化)可以替换为:但是回到错误..以及更多 Rness...下面的代码...可以
更正确地替换为吗?
(对其余代码进行类似的重新思考也可以消除其余循环)
The brief answer is to first declare a variable. Put it before everything you've posted here. I'm going to call it temp. It will hold all of the values.
Then, instead of your print line use
As an aside, your code is doing some weird things. Look at the first index of p_0y. It is effectively an index to item
0,
in that matrix. R starts indexing at 1. When you create the number of rows in that matrix you usemax(y) - min(y)
. If the max is 10 and the min is 1 then there's only 9 rows. I'm betting you really wanted to add one. Also, your code is very un R-like with all of the unnecessary while loops. For example, your whole last loop (and the initialization of logvect) can be replaced with:But back to the errors.. and some more Rness... could the following code...
maybe be replaced more correctly with?
(similar rethinking of the rest of your code could eliminate the rest of the loops as well)