I have a variable name saved into the string variable which_id.
W is a data.table. How do I call setkey on W with which_id ?
This is what I've tried
> eval( paste( 'setkey(W,' , which_id , ')' ) )
[1] "setkey(W, 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 key still didn't take.
> setkeyv( W , cols=which_id )
and
> setkeyv( W , cols=c( which_id ) )
--> same thing, customer_id_A key isn't there.
Any pointers?
evalstuff will be needed. Your last try, withsetkeyvshould work, I think. Anyway, the way I usually do evals is witheval(parse(text="..."))whereas you are doingeval("..."). - Frankparsecall did the trick, thanks. I was wondering if there was a 'data.table-native' way. - user2105469setkeyv(W, which_id)works for me, you may want to make your question reproducible - put in specificWandwhich_id- eddi