Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Is there an R function for adding categories to simulated data?

$
0
0

I have the mean and the standard deviation of 8 different schools from Gelman's school data example for hierarchical models. The thing is that you only have 8 observation and since I have the mean and the standard deviations, I simply wanted to simulate data for multiple classes within schools.

This is the current code for 30 classes each for 8 schools:

nj = 30

set.seed(1)
testA <- rnorm(nj, mean = 28, sd = 15)
A = vector(length = nj)
names(testA) <- c("A")
set.seed(2)
testB <- rnorm(nj, mean = 8, sd = 10)

set.seed(3)
testC <- rnorm(nj, mean = -3, sd = 16)

set.seed(4)
testD <- rnorm(nj, mean = 7, sd = 11)

set.seed(5)
testE <- rnorm(nj, mean = -1, sd = 9)

set.seed(6)
testF <- rnorm(nj, mean = 1, sd = 11)

set.seed(7)
testG <- rnorm(nj, mean = 18, sd = 10)

set.seed(8)
testH <- rnorm(nj, mean = 12, sd = 18)

How can I create a 1x30 vector with the only "A"s as a label to merge them with the data for the first schools and then "B"s for the second school, and so on?

typing in c("A", "A","A","A", .., "A",) seems too slow.

After having 2x30 matrixes I'd like to merge them all into 1 bigger dataframe which should be fairly easy with a merge function, I think.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>