Hi,
I would like to pass a string of column names through a loop. So that if these variables do not exist to
create columns with the same names (and fill them with NAs). This is all part of a function.
My common approach is to use := operator from data.table package. But, when I try doing this inside a function R throws me an error:
Error in `:=`(columns[i], NA) : Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
Same thing even if I'm setting the data to data.table with setDT() and data.table inside the function. Short replicable example below.
Data <- t(c(n1 = 10, n2 = 35, n3 = "text"))
columns <- c("n1", "n5")#string of col names to check
function <- colCreate(Data){
for (i in (1:length(columns))){
Data <- data.table(Data)
if(sum((colnames(Data) == paste(columns[i])))==0){
Data[,columns[i] := NA] }
}
}
#Calling the function
colCreate(Data)
Would appreciate any hints.
Thanks!