Search code examples
rdataframeconditional-formatting

Conditional fill a value in Data frame based on ranges


I want to make groups according to age ranges in a data frame. I tried:

DF$AgeGroup <- ifelse(DF$Age >= 20 & DF$Age <= 29, "G2", NA)
DF$AgeGroup <- ifelse(DF$Age >= 30 & DF$Age <= 39, "G3", NA)
DF$AgeGroup <- ifelse(DF$Aag >= 40 & DF$Age <= 49, "G4", NA)
...

but the next line replaces data assigned by the previous code. I could manage the groupings into separate columns and then merge them but there must be a simple and straightforward way to do so. Can anyone help?


Solution

  • DF <- data.frame(Age = sample(1:100, 25))
    
    cuts <- c(0, 20, 30, 40, 50, 100)
    groups <- c("G1", "G2", "G3", "G4", "G5")
    
    DF$AgeGroup <- cut(DF$Age, breaks = cuts, labels = groups, right = FALSE)
    
    DF
    
    #    Age AgeGroup
    # 1   87       G5
    # 2   88       G5
    # 3   61       G5
    # 4    8       G1
    # 5   29       G2
    # 6   25       G2
    # 7   55       G5
    # 8   44       G4
    # 9   76       G5
    # 10  86       G5
    # 11  30       G3
    # 12  99       G5
    # 13  95       G5
    # 14  62       G5
    # 15  70       G5
    # 16  82       G5
    # 17  97       G5
    # 18  50       G5
    # 19  84       G5
    # 20  35       G3
    # 21  60       G5
    # 22  20       G2
    # 23   2       G1
    # 24  19       G1
    # 25  33       G3