R函数中的比较(1)仅对原子类型或列表类型返回可能

时间:2019-02-16 13:15:33

标签: r function operation

我具有以下功能:

 agegroup <- function(data, column) {

 setDT(data)

 if (class(data[[column]] == "integer")) {

  data[column <20, agegroup := "0-19"]
  data[column >= 20 & column < 25, agegroup := "20-24"]
  data[column >= 25 & column <30, agegroup := "25-29"]
  data[column >= 30 & column <35, agegroup := "30-34"]
  data[column >= 35 & column <40, agegroup := "35-39"]
  data[column >= 40 & column <45, agegroup := "40-44"]
  data[column >= 45 & column <50, agegroup := "45-49"]
  data[column >= 50 & column <55, agegroup := "50-54"]
  data[column >= 55 & column <60, agegroup := "55-59"]
  data[column >= 60 & column <65, agegroup := "60-64"]
  data[column >= 65 & column <70, agegroup := "65-69"]
  data[column >= 70 & column <75, agegroup := "70-74"]
  data[column >= 75 & column <80, agegroup := "75-79"]
  data[column >= 80 & column <85, agegroup := "80-84"]
  data[column >= 85, agegroup := "85+"]

  return(data)

  } else {

    print("Column type is not integer.")

  }
}

该函数应检查引用的列是否为整数类型。但是,当我进行比较时,出现以下错误:

Error in data[[column]] == integer : 
  comparison (1) is possible only for atomic and list types

编辑:

我将原始功能放在上面。

此外,数据样本:

structure(list(age = c(87L, 70L, 69L, 68L, 68L, 68L)), class = c("data.table", 
"data.frame"), row.names = c(NA, -6L))

函数调用:

test <- agegroup(test, "age")

我不确定是什么问题。

0 个答案:

没有答案