Best way to Loop through each row of a dataframe to generate random variables

Hi,
I am new to R and need some help figuring out a problem.
In summary, I have a table with different values : 10 rows and 6 columns. Each column represents a variable: column 1- n1, column2- n2, column3- varaince1, column4- n2, column5- mean2, column6- variance2. Each row is a different combination of these variables.
I want to iterate through each row and generate two samples- sample 1- random normal variables with n1,mean1 and sd1 (variance1 sqrt) and sample 2-random normal variables with n1,mean1 and sd1 (variance1 sqrt).
Can someone let me know what would be the best way to proceed? Thanks for the help.

Hi Hope I understood your requirements

n1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
n2 = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11)
mean1 = c(1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8)
variance1 = c(0.5, 0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59)
mean2 = c(1.25, 1.4, 1.55, 1.7, 1.85, 2, 2.15, 2.3, 2.45, 2.6)
variance2 = c(0.75, 0.76, 0.77, 0.78, 0.79, 0.8, 0.81, 0.82, 0.83, 0.84)

mydata <- data.frame(n1,n2,mean1,variance1,mean2,variance2)

sample1=c()
sample2=c()
for (i in 1: nrow(mydata)){
sample1 <- c(sample1,rnorm(mydata[i,'n1'],mydata[i,'mean1'],sqrt(mydata[i,'variance1'])))
sample2 <- c(sample2,rnorm(mydata[i,'n2'],mydata[i,'mean2'],sqrt(mydata[i,'variance2'])))
}
sample1
sample2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.