Repeat a subsetting function by adding a new subsetting variable at each round in R

后端 未结 1 940
谎友^
谎友^ 2021-01-16 04:23

I have a function (foo) to subset any variable from the list L. It works perfect! But can I by default add variable weeks to whatever

相关标签:
1条回答
  • 2021-01-16 04:31

    You could do:

    foo <- function(List, what, time = 1){     
      s <- substitute(what)
      s <- bquote(.(s) & weeks == time)
      h <- lapply(List, function(x) do.call("subset", list(x, s)))
     h1 <- Filter(NROW, h)      
     h2 <- lapply(List[names(h1)], function(x) subset(x, control))
     Map(rbind, h1, h2)      
    }
    
    # AND THEN:
    lapply(1:3, function(i) foo(L, type == 1, time = i))
    
    0 讨论(0)
提交回复
热议问题