简体   繁体   中英

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:

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

Here is the assignment I was given:

You have a 100m x 100m grid with 1m x 1m cells. You want to select about 30% of the cells to sample. 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. Simulate the number of snails you count in each grid cell you sample based on an average snail density of 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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