条件的长度> 1,只有第一个元素将在R

时间:2017-03-29 07:21:08

标签: r if-statement

我试图从R中的简单数据中找出年龄范围。但我收到以下错误。 “条件长度> 1,只使用第一个元素”

我的数据:

YEAR
47
31
30
41
31
32

我试图找出YEAR专栏的年龄范围。

data$YEAR <- getAgeRange(data$YEAR)

getAgeRange函数就是这样:

 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)
}

我收到以下错误:

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

0 个答案:

没有答案