I have 2 data frames
lookup_table <- data.frame(Country = c("UK","France", "Germany"), A = c(0,0,1), B = c(1,6,7), C = c(4,8,9))
df <- data.frame(Country = c("UK","UK","France","France","Germany","Germany","Germany","France","UK"), Values = runif(9, 1, 10))
I would like to have a 3rd column in df which assigns class based on the values in column 2, AND on the country.
Something like the below, but the values should not be fixed: they should depend on the values & country in the lookup table
Class <- function(x) {
if(x > 0 & x <= 1) y <- "A"
if(x > 1 & x <= 4) y <- "B"
if(x > 4) y <- "C"
return(y)
}
df$Class <- sapply(df$Values,Class)
Thanks in advance for any help
Aucun commentaire:
Enregistrer un commentaire