Search code examples
rdataframeloopssapply

How to use Sapply in multiple dataframms


I have three dataframes: AAA, BBB, CCC, I want to use sapply to browse the max of each dataframe, instead of using

sapply(AAA, max)
sapply(BBB, max)
sapply(CCC, max)

I tried to use a loop to solve this function, I tried the following lines, but didn't work

dfs = list(AAA, BBB, CCC)

for(df in dfs){
  print(deparse(substitute(df)))
  print(sapply(df, max))
  readline(prompt="Press [enter] to continue")
}

I intended to use print(deparse(substitute(df))) to print out AAA, BBB, CCC. However, all I got is df.

I wonder how to fix this problem.

Or is there any other way to apply sapply to multiple dataframes at the same time?


Solution

  • We may loop over the list of data.frames and then use sapply on the dataset

    lapply(dfs, \(dat) sapply(dat, max))
    

    Or another option is colMaxs

    library(matrixStats)
    lapply(dfs, \(dat) colMaxs(as.matrix(dat)))
    

    If we want to print the object names, it should be a named list and then loop over either the sequence of list or the names of the list and print the names

    dfs <- list(AAA = AAA, BBB = BBB, CCC = CCC)
    for(nm in names(dfs)) {
       print(nm)
       print(sapply(dfs[[nm]], max))
    }