在R data.table中动态设置动态选定列的名称标签

时间:2017-04-06 14:24:31

标签: r dynamic data.table

在对data.table进行子集化时,有没有办法动态设置列的名称(或更常见的多列),动态选择原始列?

注意在这种情况下,我不是要尝试将新列分配给原始data.table。

x <- data.table(a = 1:10, b = letters[1:10])

colname <- "b"
newcolname <- "VaR"

# The name of the column dymamically selected is an ugly 'V2'
x[a > 7, list(a, get(colvar))]
#      a V2
# 1:  8  h
# 2:  9  i
# 3: 10  j



# I want to provide the new name of the column dynamically, but don't know how to do this.  
# This clearly doesn't work, but demonstrates the spirit of what I want to achieve:
x[a > 7, list(a, (newcolname) = get(colvar))]

我想实现这一点,所有这些都在data.table one liner:

#     a VaR
# 1:  8  h
# 2:  9  i
# 3: 10  j

鉴于两个好的答案,我决定将最佳答案设为最快的方法:

> microbenchmark(setnames(x[a > 7, .(a, .SD), .SDcols = colname], 2, newcolname)[],
+ x[a > 7, setNames(list(a, get(colname)), c("a", newcolname))])
Unit: microseconds
                                                              expr     min       lq      mean    median        uq      max neval cld
 setnames(x[a > 7, .(a, .SD), .SDcols = colname], 2, newcolname)[] 974.519 1003.993 1063.5964 1031.4020 1133.5330 1428.975   100   b
     x[a > 7, setNames(list(a, get(colname)), c("a", newcolname))] 402.693  436.531  485.8323  453.9695  493.3325 1752.296   100  a 

2 个答案:

答案 0 :(得分:3)

只需动态设置列表名称

{{1}}

{{1}}功能允许您命名列表中的元素。

答案 1 :(得分:2)

我们可以尝试

 setnames(x[a > 7, .(a, .SD), .SDcols = colname], 2, newcolname)[]