Search code examples
rrankingmatching

match common rows from ranked dataframes


Could anyone help me to match three or more different ranked df to have a final one containing only the rows common to all of them? I am trying match and merge functions but I can not go any further.

here is how the data may look like:

A <- data.frame(letter=LETTERS[sample(10)], x=runif(10))
B <- data.frame(letter=LETTERS[sample(10)], x=runif(10))
C <- data.frame(letter=LETTERS[sample(10)], x=runif(10))

"letter" is however the "row.names" on each df has only one column with the numerical "x", the ranked values.


Solution

  • Create data

    A <- data.frame(letter=LETTERS[sample(10)], x.A=runif(10))
    B <- data.frame(letter=LETTERS[sample(10)], x.B=runif(10))
    C <- data.frame(letter=LETTERS[sample(10)], x.C=runif(10))
    

    Find intersecting letters in all the data.frames

    vec.intersect <- Reduce(function(x, y) intersect(x, y), list(A[,1], B[,1], C[,1]), accumulate = FALSE)
    

    Merge intersecting data.frames

    df.intersected <- Reduce(function(x, y) merge(x, y, by = "letter"), list(A[with(A, letter %in% vec.intersect),], B, C), accumulate = FALSE)