0

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
Vitalijs
  • 938
  • 7
  • 18
  • Could you also show your desired output? – Frank Jan 26 '17 at 17:38
  • 1
    I guess `dropem = lapply(data, function(x) sum(nchar(gsub("&|FIELD_NOT_FOUND|\\s", "", x)), na.rm=TRUE)) == 0` then `data[, which(dropem) := NULL]` – Frank Jan 26 '17 at 17:41
  • @Frank This is what I am doing now, I was thinking whether there is more beautiful solution, that is via set in one step and not in two steps. – Vitalijs Jan 26 '17 at 17:43
  • Yeah, you're free to do it in one step -- write the code for dropem where it appears in the second line. – Frank Jan 26 '17 at 17:43
  • @Frank can you post this as an answer – Vitalijs Jan 26 '17 at 17:49
  • I can't post an answer after closing it as a dupe. Also, no real point in posting it, since you already have your filtering condition in your post; and the issue of how to drop cols idiomatically is covered in the linked q&a. – Frank Jan 26 '17 at 17:53
  • @Frank, I do not think this was a duplicate, i.e. the previous question was about how to remove multiple columns, this was about how to remove columns based on condition. – Vitalijs Jan 26 '17 at 18:11
  • You already knew the conditions. It is trivial to recognize the vector of relevant columns using the function you've already written. If someone else wants to de-dupe and answer it, they can go ahead. – Frank Jan 26 '17 at 19:53

0 Answers0