Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use `for loop` at once without non-numeric errors?

I wonder how for loop can be used at once without non-numeric error. I would like to make multiple character values in a vector Nums, using for loop.

But after the third line, the vector becomes chr so cannot continue the rest. This comes out to be same even when I use if loop or while loop… Can someone give a hint about this?

  for(n in 1:30){
    Nums<-1:n
    Nums[Nums%%2==0 & Nums%%3==0]<-"OK1"
    Nums[Nums%%2==0 & Nums%%3!=0]<-"OK2"
    Nums[Nums%%2!=0 & Nums%%3==0]<-"OK3"
    Nums[Nums%%2!=0 & Nums%%3!=0]<-n
  }
Error in Nums%%2 : non-numeric argument to binary operator

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Character and numeric values can’t coexist in a vector*. As @Ands. points out, you don’t really need a loop for this. If you want to avoid case_when (which is from the dplyr package, part of the "tidyverse"), you can do:

n <- 30
Nums <- 1:n
x <- as.character(Nums)
x[Nums%%2==0 & Nums%%3==0]<-"OK1"
x[Nums%%2==0 & Nums%%3!=0]<-"OK2"
x[Nums%%2!=0 & Nums%%3==0]<-"OK3"

You don’t need the final statement because the remaining elements were already set to the corresponding numeric values.

If you want to use a for loop and replace as you go, you could convert the vector to a list:

Nums <- 1:n
Nums <- as.list(Nums)
for (i in 1:n) {
   if (i%%2==0 & i%%3==0) Nums[[i]] <- "OK1"
   if (i%%2==0 & i%%3!=0) Nums[[i]] <- "OK2"
   if (i%%2!=0 & i%%3==0) Nums[[i]] <- "OK3"
}
unlist(Nums)

* Technically they can’t coexist in an atomic vector — lists are vectors too …

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading