jeudi 17 novembre 2016

R - write value into variable after two statements

I have a for loop with two if statements. it all works fine, except that I would like to add the values passing the second if statement to the variable v1. I assign the variable first and the variable gets filled with a lot of NA's and the three values I am expecting. But I want only those three values in the variable.

So instead of writing only the i that fulfills the requirements, every single i gets written into the variable.

v1 <- c()

for(i in seq_along(y$values)){
  if(y$lengths[i] < 500 && y$values[i] == TRUE){
    y$values[i] <- FALSE
  }
  if(y$lengths[i] > 500 && y$values[i] == TRUE){
    print(y$lengths[i])
    v1[[i]] <- y$lengths[i]
  }
}

I had a look at the apply functions, but couldn't really figure out how to use both if statements in apply.

Aucun commentaire:

Enregistrer un commentaire