Search code examples
rsubset

How to subset multiple columns using a list or a data frame with the values to subset?


I have list (with uneven number of elements) :

my.list = list(col1 = c("CC", "CT", "TT"), 
     col2 = c("GG", "GT"), 
     col3 = c("CC", "CT"),
     col4 = c("CC", "CG", "GG"), 
     col5 = c("AC", "CC"),
     col6 = "GG")

$col1
[1] "CC" "CT" "TT"

$col2
[1] "GG" "GT"

$col3
[1] "CC" "CT"

$col4
[1] "CC" "CG" "GG"

$col5
[1] "AC" "CC"

$col6
[1] "GG"

Which can be transformed into a data frame:

mylist.df = plyr::ldply(my.list, rbind)
names(mylist.df) <- c("cols","g1", "g2", "g3")

And I want to subset the data frame below using mylist or mylist.df. Basically, I want to keep every row that have at least one element of each value in mylist :

df.to.subset = structure(list(IDs = c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6"), 
               gr = c("gr1", "gr1", "gr1", "gr1", "gr1", "gr1"), 
               var = c(-3.451, -3.469, -3.837, -3.344, -3.904, -3.943), 
               col1 = structure(c(1L, 2L, 3L, 1L, 2L, 2L), levels = c("CC", "CT", "TT"), class = "factor"), 
               col2 = structure(c(1L, 1L, 2L, 3L, 3L, 3L), levels = c("GG", "GT", "TT"), class = "factor"), 
               col3 = structure(c(1L, 2L, 1L, 1L, 1L, 1L), levels = c("CC", "CT"), class = "factor"), 
               col4 = structure(c(2L, 2L, 2L, 2L, 2L, 2L), levels = c("CC", "CG", "GG"), class = "factor"), 
               col5 = structure(c(1L, 2L, 2L, 2L, 2L, 2L), levels = c("AC", "CC"), class = "factor"), 
               col6 = structure(c(1L, 1L, 2L, 1L, 1L, 1L), levels = c("GG","AA"), class = "factor")), 
          row.names = c(NA, 
                        -6L), class = c("tbl_df", "tbl", "data.frame"))

  IDs   gr      var col1  col2  col3  col4  col5  col6 
  <chr> <chr> <dbl> <fct> <fct> <fct> <fct> <fct> <fct>
1 ID1   gr1   -3.45 CC    GG    CC    CG    AC    GG   
2 ID2   gr1   -3.47 CT    GG    CT    CG    CC    GG   
3 ID3   gr1   -3.84 TT    GT    CC    CG    CC    AA   
4 ID4   gr1   -3.34 CC    TT    CC    CG    CC    GG   
5 ID5   gr1   -3.90 CT    TT    CC    CG    CC    GG   
6 ID6   gr1   -3.94 CT    TT    CC    CG    CC    GG   

(The end result would be)

  IDs   gr      var col1  col2  col3  col4  col5  col6 
  ID1   gr1   -3.45 CC    GG    CC    CG    AC    GG   
  ID2   gr1   -3.47 CT    GG    CT    CG    CC    GG   

Also, I'd like to re-level each column in the df.to.subset to match the levels in this data frame:

factor.levels.cols = structure(list(cols = c("col1", "col2", "col3", "col4", "col5", "col6"), 
               g1 = c("CC", "GG", "CC", "CC", "AA", "AA"), 
               g2 = c("CT", "GT", "CT", "CG", "AC", "AG"), 
               g3 = c("TT", "TT", "TT", "GG", "CC", "GG")), 
          row.names = c(NA, 6L), class = "data.frame")

  cols g1 g2 g3
1 col1 CC CT TT
2 col2 GG GT TT
3 col3 CC CT TT
4 col4 CC CG GG
5 col5 AA AC CC
6 col6 AA AG GG

Is a for loop mandatory here, or is there a way to make it faster? I have >1,000,000 entries to modify.


Solution

  • Below is just a base R option to make it.


    I guess you can subset the df.to.subset like below

    out <- df.to.subset[rowMeans(mapply(`%in%`, df.to.subset[names(my.list)], my.list)) == 1, ]
    

    which gives

    # A tibble: 2 × 9
      IDs   gr      var col1  col2  col3  col4  col5  col6
      <chr> <chr> <dbl> <fct> <fct> <fct> <fct> <fct> <fct>
    1 ID1   gr1   -3.45 CC    GG    CC    CG    AA    AA
    2 ID2   gr1   -3.47 CT    GG    CT    CG    AC    AA   
    

    If you want to re-level the columns, you can try

    lvls <- with(
        reshape(
            factor.levels.cols,
            direction = "long",
            idvar = "cols",
            varying = -1,
            v.names = "g"
        ),
        split(g, cols)
    )
    out[names(lvls)] <- Map(`levels<-`, out[names(lvls)], lvls)
    

    and you will see that the levels are reset to the desired levels

    > str(out)
    tibble [2 × 9] (S3: tbl_df/tbl/data.frame)
     $ IDs : chr [1:2] "ID1" "ID2"
     $ gr  : chr [1:2] "gr1" "gr1"
     $ var : num [1:2] -3.45 -3.47
     $ col1: Factor w/ 3 levels "CC","CT","TT": 1 2
     $ col2: Factor w/ 3 levels "GG","GT","TT": 1 1
     $ col3: Factor w/ 3 levels "CC","CT","TT": 1 2
     $ col4: Factor w/ 3 levels "CC","CG","GG": 2 2
     $ col5: Factor w/ 3 levels "AA","AC","CC": 1 2
     $ col6: Factor w/ 3 levels "AA","AG","GG": 1 1