选择一个列序列:`:`有效,但`seq`不可用 [英] Select a sequence of columns: `:` works but not `seq`

查看:77
本文介绍了选择一个列序列:`:`有效,但`seq`不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过从data.table中选择一些列来对数据集进行子集化。但是,我的代码无法使用某些变体。

I'm trying to subset a dataset by selecting some columns from a data.table. However, my code does not work with some variations.

这里是示例数据表。

library(data.table)
DT <- data.table( ID = 1:50,
            Capacity = sample(100:1000, size = 50, replace = F),
            Code = sample(LETTERS[1:4], 50, replace = T),
            State = rep(c("Alabama","Indiana","Texas","Nevada"), 50))

这是一个有效的子集代码,其中使用

Here is a working subset code, where a numeric sequence of columns is specified using ::

DT[ , 1:2]

但是,使用 seq same 列c $ c>不起作用:

However, specifying the same sequence of columns using seq does not work:

DT[ , seq(1:2)]

请注意,这适用于数据框,但不适用于data.table。

Note that this works with a dataframe but not with a data.table.

我需要第二种格式的内容,因为我根据 grep()的输出进行子设置,并且给出相同的结果tput作为第二种格式。我在做什么错?

I need something along the lines of the second format because I'm subsetting based on the output of grep() and it gives the same output as the second format. What am I doing incorrectly?

谢谢!

推荐答案

在最新版本的data.table中,数字可用于 j 指定列。此行为包括诸如 DT [,1:2] 之类的格式以指定列的数字范围。 (请注意,此语法在旧版本的data.table上不起作用。)

On recent versions of data.table, numbers can be used in j to specify columns. This behaviour includes formats such as DT[,1:2] to specify a numeric range of columns. (Note that this syntax does not work on older versions of data.table).

为什么 DT [,1:2] 有效,但 DT [,seq(1:2)] 不起作用吗?答案隐藏在 data.table :::: [。data.table 的代码中,其中包括以下行:

So why does DT[ , 1:2] work, but DT[ , seq(1:2)] does not? The answer is buried in the code for data.table:::[.data.table, which includes the lines:

  if (!missing(j)) {
    jsub = replace_dot_alias(substitute(j))
    root = if (is.call(jsub)) 
      as.character(jsub[[1L]])[1L]
    else ""
    if (root == ":" || (root %chin% c("-", "!") && is.call(jsub[[2L]]) && 
        jsub[[2L]][[1L]] == "(" && is.call(jsub[[2L]][[2L]]) && 
        jsub[[2L]][[2L]][[1L]] == ":") || (!length(all.vars(jsub)) && 
            root %chin% c("", "c", "paste", "paste0", "-", "!") && 
            missing(by))) {
      with = FALSE
    }

我们可以看到这里 data.table 在检测到使用功能<时会自动为您设置 with = FALSE 参数。 code>: j 中。它没有为 seq ,因此,如果我们想使用 seq 语法,就必须自己指定 with = FALSE

We can see here that data.table is automatically setting the with = FALSE parameter for you when it detects the use of function : in j. It doesn't have the same functionality built in for seq, so we have to specify with = FALSE ourselves if we want to use the seq syntax.

DT[ , seq(1:2), with = FALSE]

这篇关于选择一个列序列:`:`有效,但`seq`不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆