Search code examples
rlistdataframecriteria

R use criteria from one data frame to select elements from a list


I would like to use criteria in one data frame(df) to select elements from a list(mlist). Specifically, the df has ranges that define what groups of elements from the list I need.

For instance:

df = data.frame(Start=c(1,12,20), Stop=(4,16,22))  #there may be hundreds of these of arbitrary lengths.

mlist = c(a,b,c,d,e,f,....) 

I can select them individually like this:

sublist <- mlist[df$Start[1]:df$Stop[1]]

and I can build a for loop to loop over all the start/stop pairs and use rbind to put them together, but I'd like to avoid the for loop because of speed. Is there syntax similar to the command I list above (sublist <- mlist[...]) that could do this or is there another function (plyr?) that can use two objects (a list and a data frame) in this manner?


Solution

  • The most obvious solution (though it is a hidden loop) is to use apply, like this:

    df = data.frame(Start=c(1,12,20), Stop=c(4,16,22))
    df
    #   Start Stop
    # 1     1    4
    # 2    12   16
    # 3    20   22
    mlist <- letters
    apply(df, 1, function(x) mlist[x[1]:x[2]])
    # [[1]]
    # [1] "a" "b" "c" "d"
    #
    # [[2]]
    # [1] "l" "m" "n" "o" "p"
    #
    # [[3]]
    # [1] "t" "u" "v"