rlistselectsubsetnames

How to select elements from a list as a range of names to build a new list?


I have a list of spec_list. I would like to get a new list by keeping elements from AE:SUPPCM. How can I do that?

I can do lst2 <- spec_list[c('AE', 'SUPPAE', 'CM',' SUPPCM')], but can I do this using AE:SUPPCM instead of listing the names one by one?

str(spec_list, 1)
# List of 9
#  $ README          : chr "foobar"
#  $ SIGN_OFF        : logi TRUE
#  $ REVISION_HISTORY: logi NA
#  $ Study           : num 12345
#  $ Datasets        :List of 2
#  $ AE              : chr "yes"
#  $ SUPPAE          : int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ CM              : num 1.23e+08
#  $ SUPPCM          : int [1:3, 1:4] 1 2 3 4 5 6 7 8 9 10 ...

Solution

  • Here's a small function that can help you do that -

    select_by_names <- function(data, x, y) {
      nm <- names(data)
      data[match(x, nm) : match(y, nm)]
    }
    

    I have created my own example to show how this function works.

    tmp <- head(mtcars, 2)
    spec_list <- list(README = tmp, `SIGN-OFF` = tmp, `REVISION HISTORY` = tmp, 
                      AE = tmp, SUPPAE = tmp, CM = tmp, SUPPCM = tmp)
    
    select_by_names(spec_list, 'AE', 'SUPPCM')
    
    #$AE
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$SUPPAE
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$CM
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$SUPPCM
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    

    and another one -

    select_by_names(spec_list, 'SIGN-OFF', 'CM')
    
    #$`SIGN-OFF`
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$`REVISION HISTORY`
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$AE
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$SUPPAE
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4
    
    #$CM
    #              mpg cyl disp  hp drat    wt  qsec vs am gear carb
    #Mazda RX4      21   6  160 110  3.9 2.620 16.46  0  1    4    4
    #Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4