mercredi 29 mars 2017

The condition has length > 1 and only the first element will be used in R

I am trying to find out age range from a simple data in R. But I am getting the error below. "The condition has length > 1 and only the first element will be used"

My data:

YEAR
47
31
30
41
31
32

I am trying to find out age range from YEAR column.

data$YEAR <- getAgeRange(data$YEAR)

getAgeRange function is like that:

 getAgeRange <- function(age) {
     if (age < 15) {
          age <- "Under 15"
     } else if (age >= 15 & age <= 17) {
          age <- "15-17"
     } else if (age >= 18 & age <= 24) {
          age <- "18-24"
     } else if (age >= 25 & age <= 34) {
          age <- "25-34"
     } else if (age >= 35 & age <= 44) {
          age <- "35-44"
     } else if (age >= 45 & age <= 54) {
          age <- "45-54"
     } else if (age >= 55 & age <= 64) {
          age <- "55-64"
     } else if (age >= 55 & age <= 64) {
          age <- "55-64"
     } else if (age > 65) {
          age <- "Over 65"
     }

     return(age)
}

I am getting the error below:

Warning messages:
1: In if (age < 15) { :
   the condition has length > 1 and only the first element will be used
2: In if (age >= 15 & age <= 17) { :
   the condition has length > 1 and only the first element will be used
3: In if (age >= 18 & age <= 24) { :
   the condition has length > 1 and only the first element will be used
4: In if (age >= 25 & age <= 34) { :
   the condition has length > 1 and only the first element will be used
5: In if (age >= 35 & age <= 44) { :
   the condition has length > 1 and only the first element will be used
6: In if (age >= 45 & age <= 54) { :
   the condition has length > 1 and only the first element will be used

Aucun commentaire:

Enregistrer un commentaire