0

I have a set of data in R. 800 samples with 12 observations generated randomly between 10 and 20. From this, i have two options, if the random generated number is less than 15, then the selection is option one and above 15, its option B. Now i want to generated a set of data for option one from its mean and sd as the normal distribution where the option A is true.

Run1 <- replicate(800, rnorm(12, mean=16, sd=3.1))

Im not sure how to link my other piece of code that says whether the option A is TRUE and thus generate a value for Run1?

edit: i essentially currently have a matrix which comprises of TRUE and FALSE, for the arguments above (option A - mean=16, sd=3.1 and option B - mean=18, sd=3.3) where A is TRUE for values below 15 and FALSE for values above 15. so a matrix like this is derived (just a small sample of full matrix)

  [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  

[1,] TRUE TRUE FALSE TRUE FALSE FALSE TRUE
[2,] TRUE TRUE FALSE FALSE TRUE TRUE TRUE

So what i want now is two matrices (one for A and on for B) which provide a mean value where A is TRUE (matrix A) from a normal distribution of the option A values detailed above and a mean value for B where it is FALSE (matrix B) from a normal distribution of the option B values detailed above ideally posessing N/A or similar where the value should not be generated in the corresponding matrix.

and then finally i would also like to combine these two matrices to form a third final matrix. may seem labourious but its necessary.

Thanks

4

1 回答 1

1

一种方法是构造一个大小合适的矩阵,然后使用行索引将值填充到适当的行中......

set.seed(101)
runifvals <- runif(800,10,20)
result <- matrix(nrow=800,ncol=12)
lowvals <- runifvals<=15
## sum(lowvals) is the number of rows of data we have to generate for case A ...
result[lowvals,]  <- rnorm(sum(lowvals)*12, 16,3.1)
result[!lowvals,] <- rnorm(sum(!lowvals)*12,18,3.3)

这假设将结果作为 800x12 矩阵是可以的(但这通常可能是最方便的格式)。

如果您真的想要两个带有嵌入式 NA 的单独矩阵,请编辑(这看起来很奇怪/浪费,但无论如何......):

matA <- matB <- matrix(NA,nrow=800,ncol=12)
matA[lowvals,]  <- rnorm(sum(lowvals)*12, 16,3.1)
matB[!lowvals,] <- rnorm(sum(!lowvals)*12,18,3.3)
于 2012-07-30T00:19:00.537 回答