Hello: My problem is that I have a data frame of means, and a data frame of standard deviations which match up to each mean. I have been trying to create 500 random numbers in a given dataset for each mean/sd combination, but I am only able to generate the last value in each data set to create one dataset (there should be 28 in all). Examining my code, what can I change to generate 28 datasets based on respective mean and sds? a1<-c(178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5) a2<-c(1.69,1.3,1,.18,1.53,1.31,1.35,1.83,1.56,1.12,.74,1.48,1.67,1.53,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37) for(i in 1:length(a1)) for(j in 1:length(a2)) x<-rnorm(500,mean=a1[i],sd=a2[j]) Thank you, Agl _________________________________________________________________ Microsoft brings you a new way to search the web. Try Bing™ now gline_try bing_1x1 [[alternative HTML version deleted]]
try this:>a1<-c(178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5)>a2<-c(1.69,1.3,1,.18,1.53,1.31,1.35,1.83,1.56,1.12,.74,1.48,1.67,1.53,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37)> a.all <- expand.grid(a1,a2) > str(a.all)'data.frame': 784 obs. of 2 variables: $ Var1: num 178 178 178 178 177 ... $ Var2: num 1.69 1.69 1.69 1.69 1.69 1.69 1.69 1.69 1.69 1.69 ... - attr(*, "out.attrs")=List of 2 ..$ dim : int 28 28 ..$ dimnames:List of 2 .. ..$ Var1: chr "Var1=178.07" "Var1=178.28" "Var1=178.08" "Var1=177.74" ... .. ..$ Var2: chr "Var2=1.69" "Var2=1.30" "Var2=1.00" "Var2=0.18" ...> x <- apply(a.all, 1, function(z) rnorm(500, z[1], z[2])) # generate yournumbers> str(x) # matrix with numbersnum [1:500, 1:784] 177 174 181 181 174 ...>On Thu, Jun 18, 2009 at 1:10 PM, Alexandre Lockhart < alexandre_georges@hotmail.com> wrote:> > Hello: > > My problem is that I have a data frame of means, and a data frame of > standard deviations which match up to each mean. I have been trying to > create 500 random numbers in a given dataset for each mean/sd combination, > but I am only able to generate the last value in each data set to create one > dataset (there should be 28 in all). Examining my code, what can I change > to generate 28 datasets based on respective mean and sds? > > > a1<-c(178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5) > > a2<-c(1.69,1.3,1,.18,1.53,1.31,1.35,1.83,1.56,1.12,.74,1.48,1.67,1.53,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37) > for(i in 1:length(a1)) > for(j in 1:length(a2)) > x<-rnorm(500,mean=a1[i],sd=a2[j]) > > Thank you, > > Agl > > _________________________________________________________________ > Microsoft brings you a new way to search the web. Try Bing™ now > > gline_try bing_1x1 > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Try this also: a <- expand.grid(a1, a2) x <- mapply(rnorm, n = 500, mean = a[,1], sd = a[,2]) On Thu, Jun 18, 2009 at 2:10 PM, Alexandre Lockhart < alexandre_georges@hotmail.com> wrote:> > Hello: > > My problem is that I have a data frame of means, and a data frame of > standard deviations which match up to each mean. I have been trying to > create 500 random numbers in a given dataset for each mean/sd combination, > but I am only able to generate the last value in each data set to create one > dataset (there should be 28 in all). Examining my code, what can I change > to generate 28 datasets based on respective mean and sds? > > > a1<-c(178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5) > > a2<-c(1.69,1.3,1,.18,1.53,1.31,1.35,1.83,1.56,1.12,.74,1.48,1.67,1.53,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37) > for(i in 1:length(a1)) > for(j in 1:length(a2)) > x<-rnorm(500,mean=a1[i],sd=a2[j]) > > Thank you, > > Agl > > _________________________________________________________________ > Microsoft brings you a new way to search the web. Try Bing™ now > > gline_try bing_1x1 > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. > >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Alexandre did say 28 datasets, not 784 (28 * 28).... Thus, either: mapply(rnorm, n = 500, mean = a1, sd = a2) or apply(cbind(a1, a2), 1, function(x) rnorm(500, x[1], x[2])) HTH, Marc Schwartz On Jun 18, 2009, at 12:51 PM, Henrique Dallazuanna wrote:> Try this also: > > a <- expand.grid(a1, a2) > x <- mapply(rnorm, n = 500, mean = a[,1], sd = a[,2]) > > On Thu, Jun 18, 2009 at 2:10 PM, Alexandre Lockhart < > alexandre_georges at hotmail.com> wrote: > >> >> Hello: >> >> My problem is that I have a data frame of means, and a data frame of >> standard deviations which match up to each mean. I have been >> trying to >> create 500 random numbers in a given dataset for each mean/sd >> combination, >> but I am only able to generate the last value in each data set to >> create one >> dataset (there should be 28 in all). Examining my code, what can I >> change >> to generate 28 datasets based on respective mean and sds? >> >> >> a1<- >> c >> (178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5 >> ) >> >> a2<- >> c >> (1.69,1.3,1 >> ,. >> 18,1.53,1.31,1.35,1.83,1.56,1.12 >> ,. >> 74,1.48,1.67,1.53 >> ,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37) >> for(i in 1:length(a1)) >> for(j in 1:length(a2)) >> x<-rnorm(500,mean=a1[i],sd=a2[j]) >> >> Thank you, >> >> Agl
On Jun 18, 2009, at 1:10 PM, Alexandre Lockhart wrote:> > Hello: > > My problem is that I have a data frame of means, and a data frame of > standard deviations which match up to each mean. I have been trying > to create 500 random numbers in a given dataset for each mean/sd > combination, but I am only able to generate the last value in each > data set to create one dataset (there should be 28 in all). > Examining my code, what can I change to generate 28 datasets based > on respective mean and sds? > > a1<- > c > (178.07,178.28,178.08,177.74,177.04,178.17,177.58,57.71,59.6,60.92,59.48,59.32,61.59,59.94,28.9,29.82,30.73,25.68,27.93,28.98,29.76,123.48,127.27,127.8,127.2,127.13,126.71,125.5 > ) > a2<- > c > (1.69,1.3,1 > ,. > 18,1.53,1.31,1.35,1.83,1.56,1.12 > ,. > 74,1.48,1.67,1.53 > ,.95,.87,0.03,1.12,1.95,1.22,1.04,1.64,1.83,1,1,1.08,1.35,2.37)# the expand.grid and apply solutions have much to recommend them # but your strategy could have worked with a few minor modifications # initialize an array to hold the results x <- array( , dim= c(length(a1), length(a2), 500) )> for(i in 1:length(a1)) { > for(j in 1:length(a2)) { > x[i,j, ] <-rnorm(500,mean=a1[i],sd=a2[j])# need to close the loop bodies } } > str(x) num [1:28, 1:28, 1:500] 182 182 180 179 179 . > head(x) [1] 182.4004 181.6187 180.3283 179.2654 178.5974 176.3728 David Winsemius, MD Heritage Laboratories West Hartford, CT