lundi 10 mai 2021

Filling in a data frame using else if

I am trying to automate a process using R so it can be done rapidly with new data. I have a data frame with an empty label column, that can have 4 labels based on the size of the value in column 1.

I currently have this

testdata <- as.data.frame(document)
COL1 <- testdata$COL1
Label <- testdata$Label

for(i in 1:length(COL1)){
  if(i > 25){
    Label <- "Label 1"
  } else if(i <= 25){
    Label <- "Label 2"
  } else if(i <= 15){
    Label <- "Label 3"
  } else if (i < 7){
    Label <-" Label 4"
  }
}

The issue I am having is the whole column is filling in as "Label 1" (the i > 25 data label) no matter which order I put them in they all fill in with "Label 1".

I am assuming there is an error with the i > 25 statement but I can't see it.

Thank you

Aucun commentaire:

Enregistrer un commentaire