0

Code is running fine in R, but getting an "argument is of length zero" error when I run in R Markdown. Just running a loop to split a probability output into 0 or 1 at 50%.

Looked through some similar posts but didn't find anything about issues when moving to markdown.

for (i in 0: (nrow(test)-1)){
  i <- i+1
  if (test$pred_basemodel[i] < 0.5){
    test$pred_basemodel[i] <- 0
  }
  else {
    test$pred_basemodel[i] <- 1
  }
}

Thanks for any suggestions!

  • While I understand that your code is from an r-markdown document, there is nothing else in the question that suggests that fact has anything to do with it (or with markdown in general). If there is something relevant, then please include more context (within at least a reproducible portion of your Rmd document) so that we can see the context. – r2evans Feb 22 '20 at 22:06

1 Answers1

0

I think you can remove i <- i+1 in the for loop, since i is the iterator which will run through 1:nrow(test) automatically. Also, you can use 1:nrow(test) instead of 0:(nrow(test)-1) as the for loop condition, since your indexing naturally starts from 1.

You can try the code below

for (i in 1:nrow(test)){
  if (test$pred_basemodel[i] < 0.5){
    test$pred_basemodel[i] <- 0
  }
  else {
    test$pred_basemodel[i] <- 1
  }
}
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81