R + combine a list of vectors into a single vector

后端 未结 6 841
死守一世寂寞
死守一世寂寞 2021-01-30 10:37

I have a single list of numeric vector and I want to combine them into one vector. But I am unable to do that. This list can have one element common across the list element. Fin

6条回答
  •  半阙折子戏
    2021-01-30 10:49

    Benchmarking the two answers by Rachit and Martijn

    rbenchmark::benchmark(
      "unlist" = {
        vec<-unlist(a)
        vec[which(diff(vec) != 0)]
      },
      "reduce" = {
        a %>% reduce(c) %>% unique
      }
    )
    

    Output:

        test replications elapsed relative user.self sys.self user.child sys.child
    2 reduce          100   0.036        3     0.036    0.000          0         0
    1 unlist          100   0.012        1     0.000    0.004          0         0
    

    This one clearly beat the other one.

提交回复
热议问题