finding unique vector elements in a list efficiently

后端 未结 2 2206
心在旅途
心在旅途 2021-02-15 08:09

I have a list of numerical vectors, and I need to create a list containing only one copy of each vector. There isn\'t a list method for the identical function, so I wrote a func

2条回答
  •  心在旅途
    2021-02-15 08:40

    You could hash each of the vectors and then use !duplicated() to identify unique elements of the resultant character vector:

    library(digest)  
    
    ## Some example data
    x <- 1:44
    y <- 2:10
    z <- rnorm(10)
    ll <- list(x,y,x,x,x,z,y)
    
    ll[!duplicated(sapply(ll, digest))]
    # [[1]]
    #  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    # [26] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    # 
    # [[2]]
    # [1]  2  3  4  5  6  7  8  9 10
    # 
    # [[3]]
    #  [1]  1.24573610 -0.48894189 -0.18799758 -1.30696395 -0.05052373  0.94088670
    #  [7] -0.20254574 -1.08275938 -0.32937153  0.49454570
    

    To see at a glance why this works, here's what the hashes look like:

    sapply(ll, digest)
    [1] "efe1bc7b6eca82ad78ac732d6f1507e7" "fd61b0fff79f76586ad840c9c0f497d1"
    [3] "efe1bc7b6eca82ad78ac732d6f1507e7" "efe1bc7b6eca82ad78ac732d6f1507e7"
    [5] "efe1bc7b6eca82ad78ac732d6f1507e7" "592e2e533582b2bbaf0bb460e558d0a5"
    [7] "fd61b0fff79f76586ad840c9c0f497d1"
    

提交回复
热议问题