I'd like to add a vector to a data.table
with each name of the vector's elements corresponding to a new column, and the value repeated based on the vector's element value.
So given:
x <- data.table(a=1:2, b=3:4)
v <- c(c=5, d=6)
I'm seeking a result of:
data.table(a=1:2, b=3:4, c=5, d=6)
# a b c d
# 1: 1 3 5 6
# 2: 2 4 5 6
Things I tried:
cbind
adds the vector as a single column:
cbind(x, v)
# a b v
# 1: 1 3 5
# 2: 2 4 6
Using the approach from Adding multiple columns to a data.table, where column names are held in a vector also vectorizes wrongly (horizontally rather than vertically):
x[, (names(v)) := v]
# a b c d
# 1: 1 3 5 5
# 2: 2 4 6 6
x[, (names(v)) := list(v)]
# Same as above.