[R] Help on looping problem needed!
I am wondering if someone could help me out with following problem: I have written a for loop which generates a random normal distribution let us say 1000 times. When the restriction is met (mean0.01), the loop stops, prints the mean value and plots a histogram. for(i in 1:1000) { a-rnorm(1000,0,.2) b-abs(mean(a)) if(b.01) next else {print(b);hist(a);break}} How to reshape the loop when I want to find at least 5 distibutions that meet my restriction and save them (assign) under names R1R5. Could you help me please? Michael __ R-help@stat.math.ethz.ch 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.
Re: [R] Help on looping problem needed!
try this: tol - 0.01 mat - matrix(as.numeric(NA), 1000, 5) k - 1 while(any(is.na(mat))){ x - rnorm(1000, sd = 0.02) if (abs(mean(x)) tol) { mat[, k] - x k - k + 1 } } abs(colMeans(mat)) par(mfrow = c(2, 3)) apply(mat, 2, hist) I hope it helps. Best, Dimitris Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/(0)16/336899 Fax: +32/(0)16/337015 Web: http://med.kuleuven.be/biostat/ http://www.student.kuleuven.be/~m0390867/dimitris.htm - Original Message - From: Ing. Michal Kneifl, Ph.D. [EMAIL PROTECTED] To: Rhelp r-help@stat.math.ethz.ch Sent: Monday, July 23, 2007 4:40 PM Subject: [R] Help on looping problem needed! I am wondering if someone could help me out with following problem: I have written a for loop which generates a random normal distribution let us say 1000 times. When the restriction is met (mean0.01), the loop stops, prints the mean value and plots a histogram. for(i in 1:1000) { a-rnorm(1000,0,.2) b-abs(mean(a)) if(b.01) next else {print(b);hist(a);break}} How to reshape the loop when I want to find at least 5 distibutions that meet my restriction and save them (assign) under names R1R5. Could you help me please? Michael __ R-help@stat.math.ethz.ch 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. Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm __ R-help@stat.math.ethz.ch 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.
Re: [R] Help for looping
Please try to give a simple reproducible example and simplify your codes a bit if you want to get useful responses. For example, you say your data is a matrix of 1000*30, where I presume the matrix has 1000 rows and 30 columns. If so EMP - data[,378:392] does not make sense. Perhaps you might be interested in knn() in the class package. Regards, Adai [EMAIL PROTECTED] wrote: Rusers: I have tried to minimize computing times by taking advanage of lapply(). My data is a 1000*30 matrix and the distance matrix was created with dist(). What I am trying to do is to compute the standard distances using the frequencies attached to the nearest negibors of n reference zones. So I will have 1000 standard distances, and would like to see the frequency distribution of the standard distances. # Convert decimal degrees into UTM miles x-(data[,1]-58277.194363)*0.000621 y-(data[,2]-4414486.03135)*0.000621 # Combine x y for computing distances coords-cbind(x,y) pts-length(data) # Subset housing data and employment data RES-data[,3:17] EMP-data[,378:392] # Combine all the subdata as D D-cbind(coords,RES,EMP) cases-ncol(D)-ncol(coords) # Create a threshold bandwidth for defining the nearest neighbors thrs-seq(0,35,by=1) SDTAZ-rep(list(matrix(,nrow(D),length(thrs))),cases) for (j in 1:nrow(D)) for (k in 1:length(thrs)) for (l in 1:cases) { { { SDTAZ[[l]][j,k]- sqrt( sum( (D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2]- min(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2])+1)* ( (dis[j,as.vector(which(dis[j,]=thrs[k]))])^2 ) ) /sum(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2]- min(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2])+1) ) } } } I think that within this nested loop, I should use lapply() but I ended up getting different values I appreciate if someone could kindly help me. Thank you very much. Takatsugu Kobayashi PhD Candidate Indiana University, Dept. Geography __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Help for looping
Rusers: I have tried to minimize computing times by taking advanage of lapply(). My data is a 1000*30 matrix and the distance matrix was created with dist(). What I am trying to do is to compute the standard distances using the frequencies attached to the nearest negibors of n reference zones. So I will have 1000 standard distances, and would like to see the frequency distribution of the standard distances. # Convert decimal degrees into UTM miles x-(data[,1]-58277.194363)*0.000621 y-(data[,2]-4414486.03135)*0.000621 # Combine x y for computing distances coords-cbind(x,y) pts-length(data) # Subset housing data and employment data RES-data[,3:17] EMP-data[,378:392] # Combine all the subdata as D D-cbind(coords,RES,EMP) cases-ncol(D)-ncol(coords) # Create a threshold bandwidth for defining the nearest neighbors thrs-seq(0,35,by=1) SDTAZ-rep(list(matrix(,nrow(D),length(thrs))),cases) for (j in 1:nrow(D)) for (k in 1:length(thrs)) for (l in 1:cases) { { { SDTAZ[[l]][j,k]- sqrt( sum( (D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2]- min(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2])+1)* ( (dis[j,as.vector(which(dis[j,]=thrs[k]))])^2 ) ) /sum(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2]- min(D[as.vector(which(dis[j,]=thrs[k])),l+2]-D[j,l+2])+1) ) } } } I think that within this nested loop, I should use lapply() but I ended up getting different values I appreciate if someone could kindly help me. Thank you very much. Takatsugu Kobayashi PhD Candidate Indiana University, Dept. Geography __ R-help@stat.math.ethz.ch 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.