I have a vector of strings, and I need to create one new column in my data.table for each of them. Like so:
dt <- data.table(a = c(1,2,3), b = c(4,5,6))
column_names <- c("x", "y", "z")
I want to do something like this:
for (column_name in column_names) {
dt[, column_name := paste0(column_name, a, b)]
}
This should result in something like this:
a | b | x | y | z
-----------------------
1 | 4 | x14 | y14 | z14
2 | 5 | x25 | y25 | z25
3 | 6 | x36 | y36 | z36
But instead, it tries to create a column with the name "column_name" 3 times. How do I get around this?