I have a data.table and would like to delete all columns which satisfy some condition, I tried with using set to loop over all columns but did not manage to get the result. Small example is below. Here column three should be removed.
library(data.table)
data<-data.table(x=c("25&&&35&&1","&&&&","&&&&"),
y=c("1","1","1"),
z=c("&&&&&","","&"))
function_length_non_missing<-function(x){
x<-x[!is.na(x)]
x<-gsub("&|FIELD_NOT_FOUND","",x)
x<-gsub("\\s","",x)
sum(x!="")
}
for (col in names(data)) (set(data,j=col,value=ifelse(function_length_non_missing(data[[col]])==0,NULL,data[[col]])))
In this example It would have deleted column z:
data<-data[,z:NULL]
x y
1: 25&&&35&&1 1
2: &&&& 1
3: &&&& 1