0
votes

I am trying to use the randomly sampled letter from nbMtx$R to sample from one of the other components of the list, but I am getting an error that says I have the incorrect number of probabilities in my second attempt at sampling. I have called length(nbMtx[start]) and it returns [3], so I'm not sure why my probability vector visit_prob (which has 3 values) isn't working. I also tried replacing the prob argument in the second sample with c(.3, .3, .3) just to be sure, and that also did not work.

set.seed(514) #seed in reference to the class’ code
nbMtx <- list() #Rory’s neighborhood connections
nbMtx$R <- c("L","P","D","J")
nbMtx$L <- c("P","R","D")
nbMtx$P <- c("L","R","J")
nbMtx$D <- c("L","R","J")
nbMtx$J <- c("P","R","D")

original_prob<- rep.int(.25, 4)
visit_prob<- rep.int((1/3), 3)

start<- sample(nbMtx$R, 1, replace=FALSE, prob=original_prob)
  
visit<- sample(nbMtx[start], 1, replace=FALSE, prob=visit_prob)

Error in sample.int(length(x), size, replace, prob) : 
  incorrect number of probabilities
1

1 Answers

1
votes

Running up through the assignment to start, we see

start
# [1] "L"

Looking into the next assignment to visit that causes an error, the first argument is

nbMtx[start]
# $L
# [1] "P" "R" "D"
length(nbMtx[start])
# [1] 1

And there's the problem. Instead use [[, and you'll get a vector of the length you expect:

nbMtx[[start]]
# [1] "P" "R" "D"
visit <- sample(nbMtx[[start]], 1, replace=FALSE, prob=visit_prob)
visit
# [1] "P"