dimanche 29 mars 2020

Nested If Else Question Not Working in Dataframe

I'm working with a coloumn in a dataframe of numeric data called "L_D" which has a range of values from 0.6-1.9. I'm trying to create a new column that categorizes the range in 3 ranges: "high" for L_D>1.4, "low" for L_D<0.9, or "medium".

#add L/D grouping to CompStrngth, <0.9=Low aspect ratio; >1.4=High Aspect Ratio, else = other raio
ifelse(CompStrngthData$L_D > 1.4, CompStrngthData$L_D_group <- "high",
   ifelse(CompStrngthData$L_D > 0.9, CompStrngthData$L_D_group <- "medium",
        CompStrngthData$L_D_group <- "low" )
)

When I run this code, it produces the desired result in the console. But when I open up the dataframe, it produces a column filled with "low".

Because of that I found the cut function and it works great with the desired results

CompStrngthData$L_D_group <- cut(CompStrngthData$L_D, breaks = c(-Inf,0.9,1.4,Inf), labels = c("low", "medium", "high"))

I'm sure it's a simple mistake I'm overlooking with the dataframe or ifelse function, just looking to understand my error.

Aucun commentaire:

Enregistrer un commentaire