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 (eg grep(), c() and names()) but can't get it working ... would appreciate help :) thx
PS My "real" data frame contains 68 columns where of I would like to remove 36 (the ones in the vector).
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)]
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.