I have a data.table with a set of columns and I'm trying to create a new column as a pasted string of several of them. The number of the columns can change from time to time, but is based on a column name convention (e.g. col1:col).
I've tried to use the new {data.table} programming interface, but things aren't quite going to plan. Below is a reprex to help explain what I'm trying to achieve.
library(data.table)
dt <- data.table(id = 1:3,
col1 = letters[1:3],
col2 = letters[3:1],
col3 = LETTERS[1:3])
# id col1 col2 col3
# <int> <char> <char> <char>
# 1: 1 a c A
# 2: 2 b b B
# 3: 3 c a C
new_var <- "col_string"
var_cols <- paste0("col", 1:3)
dt[,
let(var = paste0(paste0(cols[-length(cols)], collapse = ", "), " or ", cols[length(cols)])),
env = list(
var = new_var,
cols = I(var_cols)
)]
# id col1 col2 col3 col_string
# <int> <char> <char> <char> <char>
# 1: 1 a c A col1, col2 or col3
# 2: 2 b b B col1, col2 or col3
# 3: 3 c a C col1, col2 or col3
The structure behind col_string is correct, but it would instead expect it to look like:
# id col1 col2 col3 col_string
# <int> <char> <char> <char> <char>
# 1: 1 a c A a, c or A
# 2: 2 b b B b, b or B
# 3: 3 c a C c, a or C
I'm aware that using I() in the env is the primary cause of the column names showing in my created variable, but when I remove it I get an error:
Error in list2lang(env) :
Character objects provided in the input are not scalar objects, if you need them as character vector rather than a name, then wrap each into 'I' call: [cols]
I've also tried to wrap with as.list instead of I with no success.