Is there a way to find the minimum value across multiple data frames? [closed]

If the data frames are DF1, DF2, …, DF20 then assuming you want the data frame DF such that DF[i, j] = min(DF1[i, j], DF2[i, j], …, DF20[i, j]) then:

L <- list(DF1, DF2, DF3, DF4, DF5, DF6, DF7, DF8, DF9, DF10,
          DF11, DF12, DF13, DF14, DF15, DF16, DF17, DF18, DF19, DF20)
Reduce(pmin, L)

or

do.call(pmin, L)

or

pmin(DF1, DF2, DF3, DF4, DF5, DF6, DF7, DF8, DF9, DF10,
          DF11, DF12, DF13, DF14, DF15, DF16, DF17, DF18, DF19, DF20)

We can get L in a number of ways other than writing it out:

  1. if we have the names of the data frames in a character vector Names then L <- mget(Names, .GlobalEnv) and if we knew that the data frames all have names DF followed by digits then we can form Names using Names <- ls(pattern = "^DF\\d+$")

  2. if the only data frames in the global environment are DF1, DF2, …, DFn then we can form L like this:

    m <- mget(ls(), envir = .GlobalEnv, mode = "list", ifnotfound = NA)
    L <- Filter(is.data.frame, m)
    

Leave a Comment