3

I have a data frame dataGL_all:

Date<-c("01-01-15 04:00","01-01-15 04:20","01-01-15 04:40")
FLIin<-c(96,39,72)
FLIout<-c(173,147,103)
FBEin<-c(96,116,166)
FBEout<-c(32,53,120)
dataGL_all<-data.frame(Date, FLIin, FLIout, FBEin, FBEout)

Furthermore, I have a vector:

Remove <- c("FBEin", "FLIout")

I would a piece of code that removes the columns in the vector Remove from the data frame dataGL_all. I have tried many combinations of functions (e.g. grep(), c() and names()) but can't get it working ... would appreciate help :) thx

P.S. My "real" data frame contains 68 columns where of I would like to remove 36 (the ones in the vector).

2 Answers 2

5
dataGL_all[, !names(dataGL_all) %in% Remove]

should do the trick. Or, if you want grep:

dataGL_all[, grep(paste(Remove, collapse = "|"), names(dataGL_all), invert = T)]
Sign up to request clarification or add additional context in comments.

1 Comment

It does! Thx lukA. The site tells me that I have to wait 5 minutes to accept your answer ...
4

Just to add some more possibilities, with data.table package these kind of operation are very simple. You can either temporarily remove columns using ! and with = FALSE combination. Or you can modify your data set by reference while evaluating this vector using () within data.table environment and assigning NULL to it using := assignment operator, so here goes:

Load the package and convert to a data.table class

library(data.table)
setDT(dataGL_all)

Then either do

dataGL_all[, !Remove, with = FALSE]
#              Date FLIin FBEout
# 1: 01-01-15 04:00    96     32
# 2: 01-01-15 04:20    39     53
# 3: 01-01-15 04:40    72    120

Or update the data set by reference

dataGL_all[, (Remove) := NULL][]
#              Date FLIin FBEout
# 1: 01-01-15 04:00    96     32
# 2: 01-01-15 04:20    39     53
# 3: 01-01-15 04:40    72    120

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.