This should work. You have to return an object from a function; you
can not try to reference an object within a function. So the value is
returned and saved in an object called 'rw' since that is how you are
referening it.
walk.2d<-function(n)
{
rw <- matrix(0, ncol = 2, nrow = n)
# generate t
HI Jholtman,
walk.2d<-function(n)
{
rw <- matrix(0, ncol = 2, nrow = n)
# generate the indices to set the deltas
indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))
# now set the values
rw[indx] <- sample(c(-1, 1), n, TRUE)
# cumsum the columns
rw[,1] <- cumsum(rw[, 1])
rw[,2] <- cumsum(rw[,
Please change the code to a user defined function.
--
View this message in context:
http://r.789695.n4.nabble.com/2D-Random-walk-tp3069557p3632734.html
Sent from the R help mailing list archive at Nabble.com.
__
R-help@r-project.org mailing list
https:
Hi,
I saw your code and try running it, it works!! Can you please write this
code in a user defined function.
I tried making it with making a function and cant run it.
--
View this message in context:
http://r.789695.n4.nabble.com/2D-Random-walk-tp3069557p3632459.html
Sent from the R help maili
Hello, I have a C++ code for 2D random walks with traps and I want to convert
it in a R code with its syntaxs, can anyone help???
It's easy for me to adapt the body but I want help with the beginig (variable
declaration) and th end exporting the output to a file ( like write.table() or
sin
Here is a use of color to show what the path is and following up on
Patrick's post to make the code a little more efficient:
# compute path
n <- 1
rw <- matrix(0, ncol = 2, nrow = n)
# generate the indices to set the deltas
indx <- cbind(seq(n), sample(c(1, 2), n, TRUE))
# now set the values
Hopefully someone has a good suggestion
for your question. I'd just like to
point out that the generation of the
random walk is pretty much optimized for
worst performance.
A 'for' loop is not necessary and growing
the objects could be a very significant
drag. See Circles 2 and 3 of 'The R Infe
I've wrote some code to simulate a random walk in 2 dimensions on a lattice.
Basically I want to add something in to make it plot it point by point so
you can see what is going on.
Heres my code for the random walk in 2d
RW2D<-function(N)
{
i<-0
xdir<-0
ydir<-0
xpos<-vector()
8 matches
Mail list logo