简体   繁体   English

如何基于R中另一个矩阵的值将值分配给矩阵?

[英]How to assign values to a matrix based on the values of another matrix in R?

My question may be poorly worded, but hopefully I can explain it better. 我的问题的措词可能不好,但希望我能更好地解释它。 In R, I created a matrix and assigned it values like this: 在R中,我创建了一个矩阵并为其分配了如下值:

sample<-matrix(data=rbinom(10000,1,0.3), nrow=100, ncol=100, byrow=TRUE)

Now I am trying to figure out how to assign values following a poisson distribution to each value in the matrix that == 1 现在,我试图弄清楚如何将遵循泊松分布的值分配给== 1的矩阵中的每个值

Here is the assignment I was given: 这是我的任务:

You have a 100m x 100m grid with 1m x 1m cells. 您有一个1m x 1m单元的100m x 100m网格。 You want to select about 30% of the cells to sample. 您要选择大约30%的细胞进行采样。 Simulate grid cells to sample. 模拟网格单元进行采样。 How many cells did you sample? 您采样了多少个细胞?

You count snails in each sampled grid cell. 您可以在每个采样的网格单元中计数蜗牛。 A paper you found reports an average snail density of 15/m2. 您发现的论文报告蜗牛的平均密度为15 / m2。 Simulate the number of snails you count in each grid cell you sample based on an average snail density of 15/m2. 根据平均蜗牛密度为15 / m2,在采样的每个网格单元中模拟您计数的蜗牛数量。 On average, how many snails did you count per grid cell? 您平均每个网格单元数多少只蜗牛? How many snails did you count in total across all sampled grid cells? 您在所有采样的网格单元中总共计了多少只蜗牛?

Not looking for anyone to do my work for me, just a point in the right direction would help me out a lot. 不要找任何人为我做我的工作,只是朝着正确的方向迈出了一步,这对我有很大帮助。 Thank you. 谢谢。

You can find the number of nonzero cells with: 您可以通过以下方式找到非零单元格的数量:

num_cells <- sum(sample)

At that point, you can reassign the nonzero values using rpois : 此时,您可以使用rpois重新分配非零值:

sample[sample == 1] <- rpois(num_cells, 15)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM