[R] for loop function output

Daniel Malter daniel at umd.edu
Fri Feb 12 07:59:45 CET 2010


Hi, sorry, but I cannot figure out where the five values come from. However,
generally you can define an n x 5 matrix (let's call it MATRIX), where n is
the number of simulations and 5 is the assumed number of values that each
round of the simulation returns.

Then assign the five values to the n-th row of the matrix. In the example
below, z would be your five output values.

MATRIX=matrix(0,nrow=1000,ncol=5)

for(i in 1:1000){
	x=rnorm(1)
	y=1:5
	z=x*y
	MATRIX[i,]=z
}

If need be (which it should not), you could assign MATRIX[i,j] for each j
individually, where j is in 1 to 5 and indicates the five values you want to
store. 

HTH,
Daniel

-------------------------
cuncta stricte discussurus
-------------------------
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of seydahmet ercan
Sent: Friday, February 12, 2010 1:27 AM
To: r-help at r-project.org
Subject: [R] for loop function output

Hello all,
I am trying to run a simulation. the simulation presented below.

> rep=5
> sr=.10 # selection ratio
> pmin=.10 # minority ratio
> nap=1000 # total number of applicant
> nsle=sr*nap # number of ee selected
> nb=nap*pmin # number of minority
> nw=nap-nb # number of majority
> mb=100 # mean minority
> sb=15 # sd minority
> mw=100 # mean majority
> sw=15 # sd majority
> for(i in 1:rep){
+ db=rnorm(nb,mb,sb) #minority applicant sample
+ dw=rnorm(nw,mw,sb)# majority applicant sample
+ db.t<-t(rbind(db,rep(1,nb)))  #minority applicant sample and indicator 1
+ dw.t<-t(rbind(dw,rep(0,nw))) #majority applicant sample and indicator 0
+ d<-rbind(db.t,dw.t) # combining minority and majority applicant sample
+ N1<-c(d[,1]) # nesting the data in a data frame and assigning column
labels
+ N2<-c(d[,2])
+ x<-c(1,2)
+ df<-paste("N",seq(along=x),sep="")
+ A<-data.frame(lapply(df,get))
+ names(A)<-df
+ sort1.A<-A[order(N1),]# ordering data according to column 1 (N1)
+ y<-sort1.A[(nap-nsle+1):1000,]# trim the data points for applicants who
are not hired
+ V1<-c(y[,1])# nesting the trimmed data in a data frame and assigning
column labels
+ V2<-c(y[,2])
+ z<-c(1,2)
+ hr<-paste("V",seq(along=z),sep="")
+ B<-data.frame(lapply(hr,get))
+ print(sum(V2))# sum the values in column-2 # print the simulation results
+ }

After running the simulation i get the results in the following format. I
need to get all the results as a vector (or within a data frame), not as
independent data points.

[1] 10
[1] 12
[1] 6
[1] 11
[1] 11

Thank you all in advance for your help

-- 
Seydahmet Ercan
Department of Psychology
Rice University

	[[alternative HTML version deleted]]

______________________________________________
R-help at 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.



More information about the R-help mailing list