Drop data frame columns by name

后端 未结 20 2562
花落未央
花落未央 2020-11-22 01:06

I have a number of columns that I would like to remove from a data frame. I know that we can delete them individually using something like:

df$x <- NULL
<         


        
20条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 01:53

    If you have a large data.frame and are low on memory use [ . . . . or rm and within to remove columns of a data.frame, as subset is currently (R 3.6.2) using more memory - beside the hint of the manual to use subset interactively.

    getData <- function() {
      n <- 1e7
      set.seed(7)
      data.frame(a = runif(n), b = runif(n), c = runif(n), d = runif(n))
    }
    
    DF <- getData()
    tt <- sum(.Internal(gc(FALSE, TRUE, TRUE))[13:14])
    DF <- DF[setdiff(names(DF), c("a", "c"))] ##
    #DF <- DF[!(names(DF) %in% c("a", "c"))] #Alternative
    #DF <- DF[-match(c("a","c"),names(DF))]  #Alternative
    sum(.Internal(gc(FALSE, FALSE, TRUE))[13:14]) - tt
    #0.1 MB are used
    
    DF <- getData()
    tt <- sum(.Internal(gc(FALSE, TRUE, TRUE))[13:14])
    DF <- subset(DF, select = -c(a, c)) ##
    sum(.Internal(gc(FALSE, FALSE, TRUE))[13:14]) - tt
    #357 MB are used
    
    DF <- getData()
    tt <- sum(.Internal(gc(FALSE, TRUE, TRUE))[13:14])
    DF <- within(DF, rm(a, c)) ##
    sum(.Internal(gc(FALSE, FALSE, TRUE))[13:14]) - tt
    #0.1 MB are used
    
    DF <- getData()
    tt <- sum(.Internal(gc(FALSE, TRUE, TRUE))[13:14])
    DF[c("a", "c")]  <- NULL ##
    sum(.Internal(gc(FALSE, FALSE, TRUE))[13:14]) - tt
    #0.1 MB are used
    

提交回复
热议问题