将嵌套循环的结果写入 R 中的另一个向量

发布于 2024-12-07 19:54:32 字数 1156 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缘字诀 2024-12-14 19:54:32

简短的答案是首先声明一个变量。将其放在您在此发布的所有内容之前。我将其称为临时。它将保存所有值。

temp <- numeric(1000)

然后,

temp[t] <- p_0yx %*% log2(logvect)

您的代码正在做一些奇怪的事情,而不是使用打印行。查看 p_0y 的第一个索引。它实际上是该矩阵中项目 0, 的索引。 R 从 1 开始索引。当您在该矩阵中创建行数时,您可以使用 max(y) - min(y)。如果最大值为 10,最小值为 1,则只有 9 行。我打赌你真的想添加一个。另外,您的代码与 R 非常不一样,带有所有不必要的 while 循环。例如,您的整个最后一个循环(以及 logvect 的初始化)可以替换为:

logvect = (p_0yx)/(p_yx)

但是回到错误..以及更多 Rness...下面的代码...可以

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_0y <- numeric(max(y) - min(y) + 1)
p_0y[sort(unique(y1)) - min(y1) + 1] = table(y1)/50
p_0y <- matrix(p_0y, ncol = 1)

(对其余代码进行类似的重新思考也可以消除其余循环)

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.

temp <- numeric(1000)

Then, instead of your print line use

temp[t] <- p_0yx %*% log2(logvect)

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 use max(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:

logvect = (p_0yx)/(p_yx)

But back to the errors.. and some more Rness... could the following code...

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
    }

maybe be replaced more correctly with?

p_0y <- numeric(max(y) - min(y) + 1)
p_0y[sort(unique(y1)) - min(y1) + 1] = table(y1)/50
p_0y <- matrix(p_0y, ncol = 1)

(similar rethinking of the rest of your code could eliminate the rest of the loops as well)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文