data.table:切片两个维度,字符串列列表

问题描述 投票:1回答:1

这似乎是一个非常标准的问题,但是由于我尚无法在SO上找到它,所以我认为这是一个有效的问题。

说给我一张桌子

> studTable
   age height
1:  12     48
2:  13     47
3:  14     70
4:  15     50
5:  16     62
6:  17     60
> str(studTable)
Classes ‘data.table’ and 'data.frame

以及一个包含列名的子集作为字符串的列表。

> LL = c('age', 'height')

仅使用studTable[1:5, list(age, height)]studTable怎么能和LL一样?我真正的用例是,我有很长的列(LL)列表,我想重复使用这些列以实现可维护性和可读性。

> studTable[1:5, list(age, height)]
   age height
1:  12     48
2:  13     47
3:  14     70
4:  15     50
5:  16     62
r data.table
1个回答
2
投票

您可以尝试:

studTable[1:5, ..LL]
#      age height
#  1:  12     48
#  2:  13     47
#  3:  14     70
#  4:  15     50
#  5:  16     62
© www.soinside.com 2019 - 2024. All rights reserved.