data.table:将setkey与列名变量一起使用 [英] data.table: using setkey with a column name variable
本文介绍了data.table:将setkey与列名变量一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量名保存到字符串变量 which_id
中.
I have a variable name saved into the string variable which_id
.
W
是一个数据表.如何使用 which_id
在 W
上调用 setkey
?
W
is a data.table. How do I call setkey
on W
with which_id
?
这是我尝试过的
> eval( paste( 'setkey(W,' , which_id , ')' ) )
[1] "setkey(W, customer_id_A )"
但是对 tables()
的调用显示 customer_id_A
键没有用.
But a call to tables()
shows that the customer_id_A
key didn't take.
> evalq( paste( 'setkey(W,' , which_id , ')' ) )
[1] "setkey(W, customer_id_A )"
customer_id_A
密钥仍然没有用.
> setkeyv( W , cols=which_id )
和
> setkeyv( W , cols=c( which_id ) )
->一样, customer_id_A
键不存在.
有指针吗?
推荐答案
setkeyv
应该可以.这是一个可重现的示例:
setkeyv
should work. Here is a reproducible example:
library(data.table)
W <- data.table(customer_id_A = 1:2)
which_id <- "customer_id_A"
setkeyv(W, which_id)
tables()
## NAME NROW MB COLS KEY
## [1,] W 2 1 customer_id_A customer_id_A
## Total: 1MB
这篇关于data.table:将setkey与列名变量一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文