Object disappears from namespace in function

前端 未结 1 2008
南旧
南旧 2021-01-25 12:01

I am writing a wrapper to combine any number of datasets row-wise. Since some may have unique variables, I am first restricting to the variables in the data.

My function

相关标签:
1条回答
  • 2021-01-25 12:29

    subset really shouldn't be used for these types of things. From the help page

    This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.

    For your particular problem I don't see why just replacing subset with directly using "[" would be a problem.

    rcombine <- function(List, Vars) {
      List2 <- lapply(List, "[", i= , j = Vars, drop = FALSE) # here is the change
      Reduce(rbind, List2)
    }
    
    # alternatively...
    rcombine <- function(List, Vars) {
      List2 <- lapply(List, function(x){x[, Vars, drop = FALSE]}) # here is the change
      Reduce(rbind, List2)
    }
    
    x <- data.frame('a'=sample(LETTERS, 10), 'b'=sample(LETTERS, 10), 'c'=sample(LETTERS, 10))
    y <- data.frame('a'=sample(LETTERS, 10), 'b'=sample(LETTERS, 10), 'e'=sample(LETTERS, 10))
    
    rcombine(list(x, y), c('a', 'b'))
    
    0 讨论(0)
提交回复
热议问题