# [R] for loop function output

Chris Campbell clc559 at gmail.com
Fri Feb 12 22:35:45 CET 2010

```On Fri, Feb 12, 2010 at 01:26, seydahmet ercan <seydahmetercan at gmail.com> wrote:
> 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

results<-numeric(rep) #create a vector to store your results

>> 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

results[i] <- sum(V2) #store the results in the previously created vector

> + }
>
> 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