Issues with more than one "&" condition inside if statement in R - r

I'm doing an IF statement in R to create a variable in R.
I'm having an error that I can't detect to what it refers exactly so I can't fix it. Can somebody help me?
library(install.load)
install_load("checkmate", "expss")
amostra$escol <- NA
educd003 <- data.frame("d003" = 1:9, "codeduc" = c(1,1,3,3,5,5,7,9,9))
educd0091 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(2,2,4,4,4,4,6,6,6,8,10,10))
educd0092 <- data.frame("d009" = c(1,2,3,4,5,6,7,8,9,10,11,12),
"codeduc" = c(1,1,3,3,3,3,5,5,5,7,9,9))
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Error:
Error in if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == :
missing value where TRUE/FALSE needed
Thanks,
Wagner

I solved it.
The problem, apparently, was the order.
The code below ran ok:
for (i in 1:nrow(amostra)) {
if (is.na(amostra$d001[i]) == TRUE) {
amostra$escol[i] <- 99
} else if (amostra$d001[i] == 2) {
amostra$escol[i] <- 0
} else if (amostra$d002[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d003[i], educd003, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 2) {
amostra$escol[i] <- 2
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & is.na(amostra$d014[i]) == TRUE) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 1) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0091, result_column = 2, lookup_column = 1)
} else if (amostra$d002[i] == 2 & amostra$d008[i] == 1 & amostra$d014[i] == 2) {
amostra$escol[i] <- vlookup(amostra$d009[i], educd0092, result_column = 2, lookup_column = 1)
} else {
amostra$escol[i] <- NA
}
}
Thank you very much anyway!

Related

Error: subscript out of bounds (knight's tour)

im new to R and im trying to solve for the minimum number of moves for a knight visit all the moves in a chess board.
I got the python code from:
https://www.geeksforgeeks.org/the-knights-tour-problem-backtracking-1/
and i tried to translate it to r.
But i am always getting the error and I don't know where I went wrong.
This is my code:
chess = rep(-1, times = 64)
board = matrix(data = chess, nrow = 8, ncol = 8, byrow = TRUE)
move_x = c(2, 1, -1, -2, -2, -1, 1, 2)
move_y = c(1, 2, 2, 1, -1, -2, -2, -1)
board[1, 1] = 0
pos = 1
valid_move <- function (x, y, board) {
if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
return (T)
}
return (F)
}
solve <- function (board, curr_x, curr_y, move_x, move_y, pos) {
if (pos == 64) {
return (T)
}
for (i in seq(1:8)) {
new_x = curr_x + move_x[i]
new_y = curr_y + move_y[i]
if (valid_move(new_x, new_y, board)) {
board[new_x, new_y] = pos
if (solve(board, new_x, new_y, move_x, move_y, pos+1)) {
return (TRUE)
}
board[new_x, new_y] = -1
}
}
}
main <- function() {
sims = 10
ctr = 0
number_of_moves = c()
solve(board, 1, 1, move_x, move_y, pos)
print(paste("Minimum number of moves: ", pos))
}
main()
Thanks!
The problem is that the python code relies on short-circuiting to prevent out-of-bounds errors. & will not short-circuit so you need to use &&.
Here is an example
FALSE && stop()
#> [1] FALSE
FALSE & stop()
#> Error:
Update valid_move to this
valid_move <- function (x, y, board) {
# Changed to && to allow short-circuiting
# if (x >= 1 & y >= 1 & x <= 8 & y <= 8 & board[x, y] == -1) {
if (x >= 1 && y >= 1 && x <= 8 && y <= 8 && board[x, y] == -1) {
return (T)
}
return (F)
}

How to find the Markov Chain Probability?

I am trying to find the probability that the chain jumps from state k-1 to state 1 before it hits state k.
Can anyone spot my mistake?
I tried to simulate the markov chain, but i want to make a code that allows me to find probability of k ={1, 2, 3, ........17}. But I can really not get the code.
This is the error message I always get
Error in while (X[i] > 1 && X[i] < k) { :
missing value where TRUE/FALSE needed
Here is my code:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{ i <- 1 #no. of steps
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
I think the line
i <- 1 #no. of steps
should not be there. Try this:
k <- 17
{ p <- 0.5
q <- 0.1
P <- matrix (0, nrow = k, ncol = k, byrow = TRUE)
for (i in 1:k)
{ for (j in 1:k)
{ if (i == 1 && i == j)
{ P[i,j] <- 1
}
else if (i == k && i == j)
{ P[i,j] <- 1
}
else if (i == j)
{ P[i,j] <- p*(1-q)
}
else if (j == k && i != 1)
{ P[i,j] <- q
}
else if (i == j+1 && i != k)
{ P[i,j] <- (1-p)*(1-q)
}
}
}
P
X <- (k-1)
trials <- 1000
hits <- 0 #counter for no. of hits
for (i in 1:trials)
{
while(X[i] > 1 && X[i] < k)
{ Y <- runif(1) #uniform samples
p1 <- P[X[i],] #calculating the p-value
p1 <- cumsum(p1)
# changes in the chain
if(Y <= p1[1])
{ X[i+1] = 1}
else if(Y <= p1[2])
{ X[i+1] = 2}
else if(Y <= p1[3])
{ X[i+1] = 3}
else if(Y <= p1[4])
{ X[i+1] = 4}
else if(Y <= p1[5])
{ X[i+1] = 5}
else if(Y <= p1[6])
{ X[i+1] = 6}
else if(Y <= p1[7])
{ X[i+1] = 7}
else if(Y <= p1[8])
{ X[i+1] = 8}
else if(Y <= p1[9])
{ X[i+1] = 9}
else if(Y <= p1[10])
{ X[i+1] = 10}
else if(Y <= p1[11])
{ X[i+1] = 11}
else if(Y <= p1[12])
{ X[i+1] = 12}
else if(Y <= p1[13])
{ X[i+1] = 13}
else if(Y <= p1[14])
{ X[i+1] = 14}
else if(Y <= p1[15])
{ X[i+1] = 15}
else if(Y <= p1[16])
{ X[i+1] = 16}
else if(Y <= p1[17])
{ X[i+1] <= 17}
i <- i+1
}
if(X[i]==1)
{ hits <- hits+1}
else
{ hits <- hits+0}
}
Probability <- hits/trials
Probability
}
You're setting X to k-1. In R, that's treated as a vector of length 1. As soon as i reaches 2, X[i] return an index error, because X does not have a second element.
Further notes: using the same index in two different nesting levels is very bad form. Also, when you start having a massive list of if-then-else statements, it's time to rethink your code. In this case, you could just subset 1:17 on p1[i] >=Y, take the minimum value, and then set X to that.

Getting slope in R using Loops and conditionals

I am working on a data frame with x and y columns with values as rows.. I want to calculate the slope of x and y for every 2 rows and then using the calculated slope, record whether slope's "stability" is "high" or "low". You'll understand better after seeing the code. What is wrong with this code? When I input stability, R returns NULL.
slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
stability <- c()
for (i in 1:nrow(acc)) {
if (slope[i] > 0 & slope[i] < 0.8) {
stability[i] <- "low"
} else if (slope[i] >= 0.8 & slope[i] <= 1) {
stability[i] <- "high"
} else if (slope[i] > 1 & slope[i] < 1.2) {
stability[i] <- "high"
} else if (slope[i] >= 1.2) {
stability[i] <- "low"
} else if (slope[i] >= -1 & slope[i] <= -0.8) {
stability[i] <- "high"
} else if (slope[i] >= -0.8 & slope[i] <= 0) {
stability[i] <- "low"
} else if (slope[i] < -1 & slope[i] > -1.2) {
stability[i] <- "high"
} else
stability[i] <- "low"
}
Modify your code as following:
#slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
stability <- as.vector(nrow(acc))
n <- nrow(acc)-1
for (i in 1:n) {
slope <- (acc$y[i+1] - acc$y[i]) / (acc$x[i+1] - acc$x[i])
if (slope > 0 && slope < 0.8) {
stability[i] <- "low"
} else if (slope >= 0.8 && slope <= 1) {
stability[i] <- "high"
} else if (slope > 1 && slope < 1.2) {
stability[i] <- "high"
} else if (slope >= 1.2) {
stability[i] <- "low"
} else if (slope >= -1 && slope <= -0.8) {
stability[i] <- "high"
} else if (slope >= -0.8 && slope <= 0) {
stability[i] <- "low"
} else if (slope < -1 && slope > -1.2) {
stability[i] <- "high"
} else
stability[i] <- "low"
}
Try it Online

Hanging moving gestures (Left/normal/right/up/normal/down) using mpu9150 sensor

We tried (Arduino+mpu9150) using gestures LEFT-NORMAL-RIGHT, UP-NORMAL-RIGHT.
We can able to get these gestures, but not accurately.
Its HANGING in middle, and sometimes values are also Incorrect.
Could you please help me to fix this issue?
Please check complete code for an ++attachment.
CODE:
void guesture() {
//LEFT & RIGHT
//Starting Accelerartion condition
if ( diff_yaw < -250)
{
diff_yaw = -(360 + diff_yaw);
}
else if (diff_yaw > 250)
{
diff_yaw = 360 - diff_yaw;
}
if ((aaReal.y < -1500) && (flag1 == false) && (flag2 == false) ) {
old_time1 = new_time;
old_yaw = new_yaw;
flag1 = true;
} else if ((aaReal.y > 1000) && (flag2 == false) && (flag1 == false)) {
old_time1 = new_time;
old_yaw = new_yaw;
flag2 = true;
} else if (aaReal.z > 400 && flag11 == false) {
old_time = new_time;
flag11 = true;
} else if (aaReal.z < -400 && flag22 == false) {
old_time = new_time;
flag22 = true;
}
if ((aaReal.y > 1000) && (flag1 == true) && (flagZ1 == false)) {
flagZ1 = true;
} else if ((aaReal.y < -1500) && (flag2 == true) && (flagZ2 == false)) {
flagZ2 = true;
} else if (aaReal.z < -400 && flag11 == true) {
diff_time = new_time - old_time;
flag11 = false;
if (diff_time < 2000 ) {
flag33 = true;
old_time = new_time;
}
} else if (aaReal.z > 400 && flag22 == true) {
diff_time = new_time - old_time;
flag22 = false;
if (diff_time < 2000 ) {
flag33 = true;
old_time = new_time;
}
}
if ((aaReal.y < 1000) && (flagZ1 == true)) {
flag1 = false;
flagZ1 = false;
diff_yaw = new_yaw - old_yaw;
diff_time = new_time - old_time1;
if (diff_time < 2000)
{
flagN = true;
}
} else if ((aaReal.y > -1500) && (flagZ2 == true)) {
flag2 = false;
flagZ2 = false;
diff_yaw = new_yaw - old_yaw;
diff_time = new_time - old_time1;
if (diff_time < 2000)
{
flagN = true;
}
}
if (flagN == true) {
if (Iposition == 0 && diff_yaw > 0) { //Right
Iposition = 1;
flagR = true;
} else if (Iposition == 0 && (diff_yaw) < 0 ) { //Left
Iposition = 2;
flagL = true;
} else if (Iposition == 1 && (diff_yaw) < 0 && (diff_yaw) > -25) { //Right-Normal
Iposition = 0;
flagNL = true;
} else if (Iposition == 1 && (diff_yaw) < -25) { //Right-Left
Iposition = 2;
flagL = true;
} else if (Iposition == 2 && (diff_yaw) > 0 && diff_yaw < 25) { //Left-Normal
Iposition = 0;
flagNL = true;
} else if (Iposition == 2 && diff_yaw > 25) { //Left-Right
Iposition = 1;
flagR = true;
}
}
if (flagN == true || flag33 == true) {
if (flagR == true) {
Serial.println("POSITION : RIGHT");
flagR = false;
} else if (flagL == true) {
Serial.println("POSITION : LEFT");
flagL = false;
} else if (flagNL == true) {
Serial.println("POSITION : NORMAL");
flagNL = false;
} else if (Ipos == 0 && pO >= 20 ) {
Ipos = 1;
Serial.println("POSITION : UP");
} else if (Ipos == 0 && pO <= -20 ) {
Ipos = 2;
Serial.println("POSITION : DOWN");
} else if (Ipos == 1 && pO <= 10 && pO >= -10 ) {
Ipos = 0;
Serial.println("POSITION : NORMAL");
} else if (Ipos == 1 && pO < -5 ) {
Ipos = 2;
Serial.println("POSITION : DOWN");
} else if (Ipos == 2 && pO >= -10 && pO < 0) {
Ipos = 0;
Serial.println("POSITION : NORMAL");
} else if (Ipos == 2 && pO > 0 ) {
Ipos = 1;
Serial.println("POSITION : UP");
}
flagN = false;
flag33 = false;
}
}

Question about while loops in R

FURTHER UPDATE
There's no input, because the program creates data.
The code below works for the first two patients, but I want to put it in a loop; there will be 200 patients eventually. Sorry about length.
s <- 42 #SHOULD BE 42
p <- 30 #SHOULD BE 400
p1 <- 8 #Should be 8
m <- 2 #shou;ld be higher
w <- 2
#CAPITAL LETTERS are counting versions of the lowercase
numses <- 12
mult <- 1
maxses <- round(mult*numses,0)
drop = .3; titrate = .2; complete = .3; noise = .5; other = .1
dropskip = .3; titrateskip = .2; completeskip = .3; noiseskip = .5; otherskip = .1;
dropnew = .3; titratenew = .2; completenew = .9; noisenew = .5; othernew = .1;
name = "Basic";
pdrop = .1; ptitrate = .2; pcomplete =.7; pnoise = .9; pother = 1;
# THESE HAVE TO BE ASCENDING.
w = 10
######################################################################################################################
patients <- matrix(nrow = p, ncol = s, "NA") #all patients attendance
invited <- matrix(nrow = p, ncol = s, 0) #number of sessions A or S
missinrow <- matrix(nrow = p, ncol = s, 0) #number of sessions missed in a row
insess <- vector("numeric", s) #number of people in a session
set.seed(83877201)
addlist <- rpois(s, w)
# Set up waitlist
waitlist <- vector("numeric", s)
waitlist[1] <- addlist[1]
for(i in 2:s)
{
waitlist[i] <- waitlist[i-1] + addlist[i]
}
for (i in 1:p)
{
for (j in 1:s)
{
if (i < waitlist[j]) patients[i,j] <- "W"
}
}
#Assign all patients to classes
classlist <- cut(runif(p), c(0, pdrop, ptitrate, pnoise, pcomplete, 1), labels = c("D", "T", "C", "N", "O"))
#First patient
#first session
{
invited[1,1] <- 1
insess[1] <- 1
if (classlist[1] == "D")
{
if (runif(1) < dropnew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "T")
{
if (runif(1) < titratenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "C")
{
if (runif(1) < completenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "N")
{
if (runif(1) < noisenew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < othernew) {patients[1, 1] <- 'A'} else
{
patients[1, 1] <- 'S'
missinrow[1, 1] <- 1
}
}
}
#Later sessions
for (j in 2 : s)
{
if (patients[1,(j-1)] == 'A'|patients[1,(j-1)] == 'S')
{
invited[1,j] <- invited[1,(j-1)] + 1
} else
{
invited[1,j] <- invited[1,(j-1)]
}
if (invited[1,j] <= maxses & missinrow[1,j] < m)
{
# Skip or attend
# If attended previous session
if (patients[1, (j-1)] == 'A')
{
if (classlist[1] == "D")
{if (runif(1) < drop) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "T")
{if (runif(1) < titrate) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "C")
{if (runif(1) < complete) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "N")
{
if (runif(1) < noise) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < other) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- 1
}
}
} else
# If skipped previous session
if (patients[1, (j-1)] == 'S')
{
if (classlist[1] == "D")
{
if (runif(1) < drop) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "T")
{
if (runif(1) < titrate) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "C")
{if (runif(1) < complete) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "N")
{if (runif(1) < noise) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
if (classlist[1] == "O")
{
if (runif(1) < other) {patients[1, j] <- 'A'} else
{
patients[1, j] <- 'S'
missinrow[1, j] <- missinrow[1, (j-1)] + 1
}
}
}
} else {patients[1,j] <- 'D'}
# check for number of attended or missed sessions
if (patients[1,(j-1)] == 'A' | patients[1,(j-1)] == 'S')
{
insess[j] <- 1
} else
{
insess[j] <- 0
}
}
#Second patients
#Patient is waiting and there is space
#First session
if (insess[1] < maxses & waitlist[1] > 0) #THIS MAY NEED TO BE A WHILE LOOP, FOR MULTIPLE PATIENTS
{
invited[2,1] <- 1
insess[1] <- insess[1] + 1
if (classlist[2] == "D")
{
if (runif(1) < dropnew) {patients[1, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "T")
{
if (runif(1) < titratenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "C")
{
if (runif(1) < completenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "N")
{
if (runif(1) < noisenew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < othernew) {patients[2, 1] <- 'A'} else
{
patients[2, 1] <- 'S'
missinrow[2, 1] <- 1
}
}
}
#Later sessions
#Patient invited previous session
for (j in 2 : s)
{
if (patients[2,(j-1)] == 'A'|patients[2,(j-1)] == 'S')
{
invited[2,j] <- invited[2,(j-1)] + 1
} else
{
invited[2,j] <- invited[2,(j-1)]
}
if (invited[2,j] <= maxses & missinrow[2,j] < m)
{
# Skip or attend
# If attended previous session
if (patients[2, (j-1)] == 'A')
{
if (classlist[2] == "D")
{if (runif(1) < drop) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "T")
{if (runif(1) < titrate) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "C")
{if (runif(1) < complete) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "N")
{
if (runif(1) < noise) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < other) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- 1
}
}
} else
# If skipped previous session
if (patients[2, (j-1)] == 'S')
{
if (classlist[2] == "D")
{
if (runif(1) < drop) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "T")
{
if (runif(1) < titrate) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "C")
{if (runif(1) < complete) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "N")
{if (runif(1) < noise) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
if (classlist[2] == "O")
{
if (runif(1) < other) {patients[2, j] <- 'A'} else
{
patients[2, j] <- 'S'
missinrow[2, j] <- missinrow[2, (j-1)] + 1
}
}
}
} else {patients[2,j] <- 'D'}
# check for number of attended or missed sessions
if (patients[2,(j-1)] == 'A' | patients[2,(j-1)] == 'S')
{
insess[j] <- insess[j] + 1
} else
{
insess[j] <- insess[j]
}
}
UPDATED with additional information
Thanks, Joshua, for your help so far.
Joshua asked for additional information in order to be able to help further.
The overall problem is to create a dataset of patients who attend different sessions of therapy. Whether they attend or not depends on a number of parameters, including whether they attended the previous session (which is in patients), what "class" of patient they are (which is in classlist), how many sessions they have already either attended or skipped (invited) and how many sessions in a row they have missed (missinrow); all of those will vary. If the previous session is less than full, additional patients get assigned to the new session (provided someone is waiting (waitlist).
When I do this one patient at a time, it works well. But when I try to loop over all patients, I run into the
Hello
This is one part of a larger program. I cannot figure out why it doesn't work as I thought it would. The code is
for(i in 2:p)
{
while (insess[1] < maxses & waitlist[1] > 0)
{
invited[i,1] <- 1
waitlist[1] <- waitlist[1] - 1
insess[1] <- insess[1] + 1
if (classlist[i] == "D")
{
if (runif(1) < dropnew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "T")
{
if (runif(1) < titratenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "C")
{
if (runif(1) < completenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "N")
{
if (runif(1) < noisenew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
if (classlist[i] == "O")
{
if (runif(1) < othernew) {patients[i, 1] <- 'A'} else
{
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
}
}
When I run this, i goes to 30 (which is right), insess[1] goes to 10 (also right), waitlist[1] goes to 0 (also right) but the patients matrix is only updated for i = 2. I also checked that classlist has appropriate values.
I don't see where it is leaving the loop. I think I'm using while incorrectly, but not sure how.
thanks!
You're not resetting insess[1] and waitlist[1] in each iteration of the for loop. When i = 3, insess[1] = 10 and waitlist[1] = 0, so nothing in the while loop is executed.
As as added bonus, here's a more succinct version of your code using the switch function.
for(i in 2:p)
{
insess[1] <- INITME
waitlist[1] <- INITMETOO
while (insess[1] < maxses & waitlist[1] > 0)
{
invited[i,1] <- 1
waitlist[1] <- waitlist[1] - 1
insess[1] <- insess[1] + 1
newswitch <-
switch(classlist[i],
D = dropnew,
T = titratenew,
C = completenew,
N = noisenew,
O = othernew)
if (runif(1) < newswitch) {
patients[i, 1] <- 'A'
} else {
patients[i, 1] <- 'S'
missinrow[i, 1] <- 1
}
}
}

Resources