I have a matrix and a vector of names. I want to add the elements of t to my dataframe, df. However, when I try to set multiple columns at once, my code fails. See the example below:
df = DataFrame(A=rand(4), B=rand(4))
new_col_names = ["C","D"]
t = rand(2,4)
### Does not work
df[:, new_col_names] = t'
### Works
df[:,"C"] = t[1,:]
### Works
df[:, ["A", "B"]] = t'
The bit that does not work throws the following error: ArgumentError: column name “C” not found in the data frame; existing most similar names are: “A” and “B”
I realize I can do this using a for loop, but is there a reason why multiple new columns cannot be assigned at once? And is there an alternative, aside from a for loop?
EDIT: added a transpose operator that I had forgotten to type
Might be a cleaner way to write this, but the insertcols! function takes a variable number of pairs (col_name => col_data) to add multiple columns at once.