在data.tables中使用unlist,lapply和grepl时出错

时间:2016-12-20 15:37:55

标签: r data.table

此问题是此特定question的扩展。我有这个特殊的data.table。我正在使用data.tablemc2de1071个库

library("data.table")
library("mc2d")
library("e1071")
col <- c("COST","TIME")
dt <- structure(
  list(
    ID = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"),COST_PR_L = c(NA, 0.4, 0.31, 0.4, 0.5, 0.17, 1, 0.5, 0.5, 0.5),COST_PR_U = c(7.5, 2, 2.67, 1.67, 2.4,2, 1.5, 2, 2, 1.67),COST_PO_L = c(NA, 0.33, 0.25, 0.44,0.5, 0.25, 1, 0.5, 0.5, 0.5),COST_PO_U = c(3, 1.43, 3.33,1.8, 2.4, 3.6, 1.45, 2, 1.5, 1.67), TIME_PR_L = c(NA, 0.5,0.4, 0.5, 0.5, NA, 0.67, 0.5, 0.5, 0.5), TIME_PR_U = c(2,2.5, 3, 1.5, 2, NA, 1.5, 2, 1.67, 2), TIME_PO_L = c(NA,0.4, 0.25, 0.56, 0.5, NA, 0.6, 0.5, 0.5, 0.5), TIME_PO_U = c(2,2, 5, 1.67, 2.5, NA, 1.5, 2, 1.67, 2)
  ),.Names = c("ID","COST_PR_L", "COST_PR_U","COST_PO_L","COST_PO_U","TIME_PR_L","TIME_PR_U","TIME_PO_L","TIME_PO_U"),class = c("data.table","data.frame"),row.names = c(NA,-10L))

当我在其上运行此特定操作时,

dt[, unlist(lapply(col, function(xx) {
     y = colnames(dt)[grepl(pattern = xx, x = colnames(dt))]
     vars1 = y[grepl(pattern = "PR", x = y)]
     vars2 = y[grepl(pattern = "PO", x = y)]
     mn = get(vars1[1])
     mx = get(vars1[2])
     sk1 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
     mn = get(vars2[1])
     mx = get(vars2[2])
     sk2 = ifelse(mn !=0 && mx !=0,skewness(rpert(1000, min = mn , mode = 1, max= mx )),-1)
     return(list(sk1, sk2))
 }), recursive = FALSE)
 , by = "ID"]

我收到以下错误

  

[.data.table中的错误(dt ,, unlist(lapply(col,function(xx){:
  组2的结果的第1列是“double”类型但期望类型   “逻辑”。每个组的列类型必须一致。

但是,如果我删除代码中的unlist,它似乎计算答案。什么是黑名单正在弄乱它?

0 个答案:

没有答案