[R] Getting a vector result from a computed index

Justin Long justinlong at gmail.com
Sat Mar 23 04:09:51 CET 2013


Greetings,

I confess to being new to R, which I am exploring for some simulation
modeling purposes. I am a long time programmer in PHP.

I am having some trouble getting over the "steep learning curve" with
some very basic things which are probably just little "a-ha" things in
R. I have hunted and hunted through the manual and cannot figure this
one out, so I am appealing for help.

I have the following program:

results <- replicate(10000,0)

for (year in 2000:2050) {
print(year);
for (i in 1:10000) {
x=results[i];
if (x == 0) {
prev = results[i-1];
next = results[i+1];
prob=0.1;
if (i>=1) {
if (prev==1) { prob=prob+0.4; }
}
if (i<10000) {
if (next==1) { prob=prob+0.4; }
}
y=runif(1,0,1);
if (y<prob) { x=1; }
results[i]=x;
}
}
}

No matter how I try this (and I've tried a number of variations), I
get an error similar to

Error in next = results[i + 1] : invalid (NULL) left side of assignment

I need to be able to loop through the results vector and compare the
previous/next items in the list to the current one. Is that possible?
What am I missing?

Cordially,
Justin Long



More information about the R-help mailing list