0

Good evening,

I have a dataframe with 100 rows and the column headers are ID, x-coordinate and y-coordinate:

ID X Y
1 0.1 0.1
2 0.2 0.2

and so on. I would now like to simulate a random walk between these 100 points, but do not know how to call a row. I thought about something similar to the following: dataframe[ID,]=dataframe[ID+1,]+sample(step,1)

However this throws the error "unused argument (alist(id = ))"

does somebody know how to fix this?

Many thanks in advance!

  • You should create a sample data set that is similar to the one you are using, but just make it 10 rows. Use `dput()` to include it in your question. Also define `step`. One problem is that `ID` is only defined within `dataframe` so `dataframe[ID, ]` calls an undefined variable. Something like `with(dataframe, ... )` might work. – dcarlson Jun 26 '21 at 21:57

1 Answers1

1

This will return the rows in randomized order:

set.seed(123)  # pick an arbitrary number to make reproducible
dfrm[ sample(100), ]

If you just wanted to x and y values but not the IDs, it would be:

set.seed(567)
dfrm[ sample(100), 2:3 ]

Here's a plot of a result:

df_start <- data.frame(ID=1:100, X=runif(100), Y=runif(100))
dfrm <- df_start[ sample(100) , ]
plot( x=0:1,y=0:1, plot=FALSE) # just setting range
arrows(x0=head(dfrm$X, -1), x1=tail(dfrm$X,-1), 
       y0=head(dfrm$Y, -1), y1=tail(dfrm$Y,-1) )

enter image description here

You said you wanted a "random walk between these points". The other way to create a random walk which would be more Markovian would be to use the values as increments from a starting point and have the values centered at 0, perhaps spanning [-1, 1]. You would instead use the cumsum of their values after starting at (0,0)

IRTFM
  • 258,963
  • 21
  • 364
  • 487