2

Is there possibility to write function f(), which I can use for subsetting columns set which we can use strictly like this : data[, f(c("var1", "var2", "var3"))] without using data as argument,f() could be something like :

f <- function(data_frame, minus_colnames)
     return colnames(data_frame)[which(!colnames(data_frame)%in%minus_colnames)]

but can we somehow call f without explicitly pointing to data dataframe like :

data[, f(c("var1", "var2", "var3"))]

not like

data[, f(data, c("var1", "var2", "var3"))] (and without dplyr) ?

That would be most suitable syntax for me

2
  • 2
    Also probably using setdiff(names(data), c("var1", "var2", "var3")) Commented Nov 11, 2015 at 13:13
  • 2
    I should have realized there would be a duplicate. Commented Nov 11, 2015 at 13:36

1 Answer 1

4
subset(data, select= -c(var1, var2, var3))

or

data[,!names(data) %in% c("var1","var2","var3"))]

or as @DavidArenburg suggests

setdiff(names(data), c("var1", "var2", "var3")

dplyr::select is another option; I'm sure there are data.table solutions too.

Sign up to request clarification or add additional context in comments.

2 Comments

The dplyr option could be select(data, -num_range("var", 1:3)), for example. (+1)
To finish it off dt[, !paste0('var', 1:3), with=F] and dt[, paste0('var', 1:3) := NULL] for data.table

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.