Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 剪切作为参数传递给用户定义函数的datatable变量_R_Function_Data.table_Cut - Fatal编程技术网

R 剪切作为参数传递给用户定义函数的datatable变量

R 剪切作为参数传递给用户定义函数的datatable变量,r,function,data.table,cut,R,Function,Data.table,Cut,我在“helpers.R”中有一个名为“percent_map”的用户定义函数,它接受以下参数: percent_map <- function(DT,var,statelist, pal,legend.title) 如何解决这个问题,使最终结果相同?这是str(visabystate) 但是,如果在percent_map函数中的cut内使用DT$var,则该函数不起作用。它给出了同样的错误 编辑 在cut语句中添加了=FALSE,如下所示 DT[,percents:=as.intege

我在“helpers.R”中有一个名为“percent_map”的用户定义函数,它接受以下参数:

percent_map <- function(DT,var,statelist, pal,legend.title)
如何解决这个问题,使最终结果相同?这是str(visabystate)

但是,如果在percent_map函数中的cut内使用DT$var,则该函数不起作用。它给出了同样的错误

编辑

在cut语句中添加了=FALSE,如下所示

DT[,percents:=as.integer(cut(DT[,var,with=FALSE], 5, include.lowest = TRUE))]
它提供了以下附加信息:

    percent_map <- function(DT,var,statelist, pal, legend.title) {

      # generate vector of fill colors for map
      shades <- RColorBrewer::brewer.pal(5, pal)

      # constrain gradient to percents that occur between variable range
      ##********Error in the below statement*************
      DT[,percents:=as.integer(cut(DT[,var], 5, include.lowest = TRUE))]



      names <- DT[match(map("state", plot=FALSE)$names,as.character(tolower(DT[,statelist]))),statelist]
      colorsmatched<-DT[match(names,as.character(DT[,statelist])),percents]
      fills <- shades[colorsmatched]  

#map plotting function here 
    }
The following objects are masked from DT (pos = 3):

    casesbystate, employer_state, fulltimebystate, workersbystate

The following objects are masked from DT (pos = 4):

    casesbystate, employer_state, fulltimebystate, workersbystate

The following objects are masked from DT (pos = 5):

    casesbystate, employer_state, fulltimebystate, workersbystate

 Show Traceback

 Rerun with Debug
 Error in cut.default(DT[, var, with = FALSE], 5, include.lowest = TRUE) : 
  'x' must be numeric 

提前谢谢

我认为问题来自
var='casesbystate'
。它应该是
var=casesbystate
(不带引号)。查看
visabytate[,'casesbystate']
visabytate[,casesbystate]
之间的差异。或者,如果要在对
data.table
进行子集设置时传递字符串,可以使用
with=FALSE
参数。是的,我也这样认为。但是,我需要将变量名作为参数传递,而将其作为字符串传递似乎是唯一的方法。不幸的是,with=FALSE不起作用。我已经更新了问题以显示错误,您应该开始新的会话。但是,如果需要单个列,标准的
DT[[var]]
应该可以使用。只需使用
DT[[var]]
而不是
DT[,var]
。数据表FAQ:@user227710-这是一个整数。我试图错误地提取列。
visabystate[,percents:=as.integer(cut(visabystate[,casesbystate], 5, include.lowest = TRUE))]
DT[,percents:=as.integer(cut(DT[,var,with=FALSE], 5, include.lowest = TRUE))]
The following objects are masked from DT (pos = 3):

    casesbystate, employer_state, fulltimebystate, workersbystate

The following objects are masked from DT (pos = 4):

    casesbystate, employer_state, fulltimebystate, workersbystate

The following objects are masked from DT (pos = 5):

    casesbystate, employer_state, fulltimebystate, workersbystate

 Show Traceback

 Rerun with Debug
 Error in cut.default(DT[, var, with = FALSE], 5, include.lowest = TRUE) : 
  'x' must be numeric