This may seem like a duplicate question but maybe I am missing something here.
I have been trying to remove just the columns where the sum of absolute values add to zero from a data.table.
I searched and found many solutions on this site that claim to work, and in fact, when I copy/paste exact code, it does work. However, for some reason, I can not duplicate it with my data.table.
The result of almost anything I do turns my data.table into a list. I even tried to convert my data.table to data.frame to try these solutions with no luck.
from here:
SelectVar[, colSums(SelectVar != 0) > 0]
Does not work.
SelectVar[, !apply(SelectVar == 0, 2, all)]
Does not work either.
remove_zero_cols <- function(df) {
rem_vec <- NULL
for(i in 1:ncol(df)){
this_sum <- summary(df[,i])
zero_test <- length(which(this_sum == 0))
if(zero_test == 6) {
rem_vec[i] <- names(df)[i]
}
}
features_to_remove <- rem_vec[!is.na(rem_vec)]
rem_ind <- which(names(df) %in% features_to_remove)
df <- df[,-rem_ind]
return(df)
}
This function also does not work.
I checked the class of each parameter and they are all either numeric or integer types. I also checked for any NA's and found none.
Any suggestions?