[R] Output from while and for loop
Hello, I am new and am having a hard time getting the proper syntax for output from loops. I am working on a simulation to generate a null expectation of bee behavior. Pieces of it work. The part that I am having specific difficulty is in output of a vector from within the while loop that I am using. Basically the simulation works as such: I have a starting point and a neighbor matrix and a certain threshold distance for travel. In the while loop the bee moves to a randomly chosen neighbor location. I want to be able to record the elevations of these points (including the starting point) so that I can look at variance in elevation and mean elevation. The loop itself works as does the calculation of the final elevation list, change in elevation list, and true total distance traveled. I have looked in all of the email archives but have not come across a correct way of doing it. Code below: start.elev.list-list() final.mean.elev.list-list() final.elev.list-list() final.distance.list-list() final.delta.elev.list-list() final.var.elev-list() b-length(Bees.Day.1$bee) for (bee in 1:b){ #this is for number of bees that are trackable in the day with starting points and threshold distances elev.current.vector-vector(mode=numeric, length=0) count-1 ElevSS-0 d.traveled-0 thresh-Bees.Day.1$cum.dist[bee] n-Bees.Day.1$grid.pt[bee] #I'm making this up for the threshold, want to be bee specific #current.point-round(runif(1,1,n)) #random starting point current.point-Day.1.neighbor.matrix[1,n] #I want to specify the first point in the matrix Elev.Sum-Day.1.elev.vector[current.point] while(d.traveledthresh){ #which of the four options will be selection transition-round(runif(1,1,4)) #so, what's the new point? new.point- Day.1.neighbor.matrix[transition,n] #what is the variance in elevation changed Elev.current-Day.1.elev.vector[current.point] elev.current.vector[i]-Elev.current Elev.new-Day.1.elev.vector[new.point] Elev.Sum-(Elev.Sum+Elev.new) #how far will bee travelled current.travel- Day.1.distance.matrix[current.point, new.point] d.traveled- current.travel + d.traveled current.point- new.point #Number of iterations until we reach the threshold count-count+1 } print(count) print(elev.current.vector) mean.elev-Elev.Sum/count print(paste(Final mean elev for bee, bee, is, mean.elev, sep= )) final.mean.elev.list[bee]-list(mean.elev) #What was the start elevation? start.elev-Day.1.elev.vector[n] print(paste(Start elev for bee,bee,is,start.elev, sep= )) start.elev.list[bee]-list(start.elev) #what is the final elevation? final.elev-Day.1.elev.vector[current.point] print(paste(Final elev for bee,bee,is, final.elev,sep= )) final.elev.list[bee]-list(final.elev) print(paste(Final travel distance for bee, bee,is, d.traveled, sep= )) final.distance.list[bee]-list(d.traveled) net.delta.elev-(final.elev-Day.1.elev.vector[n]) print(paste(Final net change in elevation for bee,bee,is, net.delta.elev,sep= )) final.delta.elev.list[bee]-list(net.delta.elev) } ~~ Ryan D. Briscoe Runquist Population Biology Graduate Group University of California, Davis [EMAIL PROTECTED] __ 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] Question on output of vectors from a for loop into a matrix
Hello R-help, I am a recent convert to R from SAS and I am having some difficulty with output of a for loop into a matrix. I have searched the help manuals and the archives, but I can't get my code to work. It is probably a syntax error that I am not spotting. I am trying to make a distance matrix by extracting the distances from each point to all others in a vector (the for loop). I can get them all to print and can save each individually to a file, but I cannot get them to be bound into a matrix. Here is the code that I have tried: m-length(Day.1.flower.coords$x) #31 grid points output.matix-matrix(0.0,m,m) for(i in 1:m){ dist-spDistsN1(Day.1.coords.matrix, Day.1.coords.matrix[i,]) dist-as.vector(dist) output.matrix[i,]-dist print(dist)} The error message that I get is: Error in output.matrix[i,] - dist : incorrect number of subscripts on matrix Thanks for your help. Ryan ~~ Ryan D. Briscoe Runquist Population Biology Graduate Group University of California, Davis [EMAIL PROTECTED] __ 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 with scatterplot3d
Hello, I am having a bit of trouble with scatterplot3d(). I was able to plot a 3d cloud of points using the following code: my.3dplot-scatterplot3d(my.coords, pch=19, zlim=c(0,1), scale.y=0.5, angle=30, box=FALSE) where my.coords is a data frame that contains x, y, and z coordinates for grid points whose elevation we sampled. The problem occurs when I try to add points using points3d. I tried to follow the code in the examples of the package pdf. First, I tried the following code to add all of the points that I wanted: my.3dplot$points3d(seq(400,600,0.19), seq(600,400,0.295), seq(800,500,0.24), seq(1000,1400,0.22), seq(1200,600,0.24), seq(1200,1500,0.28), seq(1300,1400,0.205), seq(1700,500,0.26), seq(1700,600,0.21), seq(1900,1400,0.255), seq(2300,1400,0.275), seq(2600,1300,0.225), seq(2700,400,0.235), seq(2700,1300,0.265), seq(3100,1000,0.135), col=blue, type=h, pch=16) and got the following error: Error in seq.default(600, 400, 0.295) : wrong sign in 'by' argument So I just tried to add the first point using the following code: my.3dplot$points3d(seq(400,600,0.19), col=blue, type=h, pch=16) and got the following error message: Error in xyz.coords(x, y, z) : 'x', 'y' and 'z' lengths differ. Does anyone know how I can use this function in the scatterplot3d package? Thanks so much! Ryan ~~ Ryan D. Briscoe Runquist Population Biology Graduate Group University of California, Davis [EMAIL PROTECTED] __ 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] simulate a null expectation
Hello, I am trying to write a simulation to generate a null hypothesis so that I can test my data. Essentially we started out with a grid of points and I have a symmetrical matrix of the distance between all of these points. I then want to simulate random movement. So I want to be able to start at a point, search through the matrix for the three closest points and with equal probability move to one of them or stay at the same place and then record the distance moved as well as the relative elevation (a vector of data that I also have) and then starting at that grid-point repeat the process a number of times. Does anyone know of a function that could help me with this? Thanks, Ryan ~~ Ryan D. Briscoe Runquist Population Biology Graduate Group University of California, Davis [EMAIL PROTECTED] __ 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.