mercredi 20 mai 2020

Adding new column with conditional values using if-else (RStudio)

I can't understand why the code isn't working. i want to add a column that for the 1/3 value of my data in a different column it will write "Low", the 2/3 of the data "Medium" and the rest "High"

OWL_DIET$skull_lenth_w_factor = c(OWL_DIET$skull_length * 1.5)

first_third <- (max(OWL_DIET$skull_lenth_w_factor))/3
second_third <- (2*(max(OWL_DIET$skull_lenth_w_factor)))/3

OWL_DIET$Level <- 1 i <- 1

for (val in OWL_DIET$Level) {if (OWL_DIET$skull_lenth_w_factor[i] < first_third) {OWL_DIET$Level[i] <- "Low" } else if (OWL_DIET$skull_lenth_w_factor[i] < second_third) {OWL_DIET$Level[i] <- "Medium" } else data$Level[i] <- "High" i <- i + 1}

Aucun commentaire:

Enregistrer un commentaire