i want to create a data.table and I want to use dynamic column names, by the way this is not about adding columns to an existing data.table.
Here is an example
outer.name <- "outer.column"
outer.members <- c("I", "II")
outer.members.cnt <- length(outer.members)
dt.outerinner <- data.table(outer = outer.members)
setnames(dt.outerinner, outer.name)
Now I want to know, if it's possible to pass the variable "outer.name" directly to data.table(... = outer.members)
and omit setnames(...)
?
Any hint is appreciated
Tom
Could this be what you want?
outer.name <- "outer.column"
outer.members <- c("I", "II")
dtfinal = data.table()
dtfinal[ , (outer.name) := outer.members]
First create a placeholder for you data.table
, then update it by reference using the usual :=
operator. Use parenthesis to pass in a column name programmatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With