Why does my list does not work

user7905871

I have a function which check for me my list. I have a list with two vectors. instead of repeating my code, I tried to use for loop. However, the for loop does not work as expected.

Here is my code:

 x <- c(2,3,4,5)
 y <- c(2,4,6,7)
 z <- list(x,y)
 pscale <- numeric()
 pscale <- list()
 for(i in 1:4){#length of my vector
 for(j in 1:2){#length of z
 pscale[[j]][[i]] <- ifelse(z[[j]][[i]] %in% c(2,9,10),0.01,1)
 }
 }
Error in `*tmp*`[[j]] : subscript out of bounds
Glaud

That should work.

x <- c(2,3,4,5)
y <- c(2,4,6,7)
z <- list(x,y)
pscale <- list()
length(pscale) <- 2 # set length to the number of vectors
for(i in 1:4){#length of my vector
  for(j in 1:2){#length of z
    pscale[[j]][i] <- ifelse(z[[j]][i] %in% c(2,9,10),0.01,1)
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related