-
Notifications
You must be signed in to change notification settings - Fork 977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
using get on a variable column breaks when variable is already a column name #3052
Comments
Are you wanting or expecting different behavior? Currently, you can do..
with the Side note: if you use names(DT) on its own, it will be modified along with DT:
#512 and https://stackoverflow.com/questions/15913417/why-does-data-table-update-namesdt-by-reference-even-if-i-assign-to-another-v?noredirect=1&lq=1 |
That names feature is bizarre, good to know. As for the issue raised, it seems to me it makes get() a potentially dangerous way of referencing variables. get(var) on its own is often suggested on stackoverflow as a solution, but could easily result in a situation where a function breaks when a DT with the variable name in its colnames is passed in. Your alternative using get(..var) is safer, but I've never seen it mentioned. Maybe some documentation around the safest wayd of handling these situations would save people some pain in the future? |
@nolanp2 There is another way using base R feature called computing on the language. Personally I actually prefer this method, as you use feature of a language you coding in.
Worth to mention it is quite exceptional feature of R language, you can read more about it in official R language manual in Computing on the language chapter. |
Basically In addition to Jan's & Frank's recommendations, you should also be able to use
Depends on what makes the most sense to you. |
I've seen get suggested as a standard way of referring to column names via a variable. In the event that the variable matches a column name in the dt, this seems to cause issues. Here's a simple example without any issues:
Now I make a slight change to dt, setting the first column name to match the variable col1:
returns error:
The text was updated successfully, but these errors were encountered: