使用字符串变量以编程方式设置键

时间:2017-08-01 20:52:09

标签: r data.table

因为setkeysubsitute

有些魔力
#>setkey
function (x, ..., verbose = getOption("datatable.verbose"), physical = TRUE) 
{
    if (is.character(x)) 
        stop("x may no longer be the character name of the data.table. The possibility was undocumented and has been removed.")
    cols = as.character(substitute(list(...))[-1])
    if (!length(cols)) 
        cols = colnames(x)
    else if (identical(cols, "NULL")) 
        cols = NULL
    setkeyv(x, cols, verbose = verbose, physical = physical)
}

我不能将字符串变量作为colname传递给setkey而不将其视为表达式:

data = data.table(mtcars)
key = 'qsec'
setkey(data, key)
# Error in setkeyv(x, cols, verbose = verbose, physical = physical) : 
# some columns are not in the data.table: key
setkey(data, 'qsec')
# Works as expected

有解决方法吗?

0 个答案:

没有答案