Search code examples
rlistfunctionparameter-passingmultiple-arguments

Creating a variable number of arguments, function


How to create a function which accepts unterminated number of arguments

In a real world example, what I would like to accomplished after knowing this information creating the function from bellow:

list.max <- function(list, ... )

where the ... represents the different columns from the data.frames, which are inside the list.

The function will compare the elements from the columns, line to line, and return a vector with the maximum value from them all.

To help the process, I already did some work. This is the closest I was able to get:

#function to return the maximum value from each line, between all the columns listed
#@Arg List: A list of data.frames which contain the columns
#@Arg col.name1 ... col.nameN: Character variable containing the names from the columns to compare
#Pre: The list must exist and the data.frames must contain the same columns
#Pos: The function will return a vector with their first element 
#  being the maximum value, between the columns listed, from the first 
#  data.frame from the list. The second element, being the maximum 
#  value between the columns listed, from the second data.frame from 
#  the list. The analogy continues until the N element

list.max <- function(list, col.name1, col.name2, ... , col.nameN){
   #creates the first data.frame with the correct amount of rows
   data.frame = data.frame(list.exapply(list, max, col.name1))

   #loop intill the end
   data.frame[1] = list.exapply(list, max,  col.name1)
   data.frame[2] = list.exapply(list, max, col.name2)
    ...
   data.frame[N] = list.exapply(list, max, col.nameN)

   #transpose the data.frame, so it can be compared in the max function, as he is casted to a matrix class
   t(data.frame)

   #creates the vector so it can storage the max value between the columns (which are now the lines)
   vet = vector()

   #storage the solution
   for( i in 1:nrow(data.frame)) {vet[i] = max(data.frame[i,])}

   #return the solution
   return (vet)
}

The auxiliary functions used above are these:

df.exapply <- function(data.frame, func, col.name){
  variavel <-func(data.frame[, col.name])
  # print(variavel)
  return (variavel)
}

list.exapply <- function(list, func, col.name){
  vet = df.exapply(list[[1]], func, col.name)
  # print(col.name)
   for (i in 1:length(list)) { vet[i] = df.exapply(list[[i]],func, col.name)
                      }
  return (vet)
}

In advance, thank you for your help!


Solution

  • So from what I've gathered, you want to have a list with x dataframes and find the maximum value of all observations and all variables in each dataframe. Why don't you do the following:

    # Create list with 10 dataframes
    df_list <- list()
    for (i in 1:10) {
      df_list[[i]] <- data.frame(matrix(rnorm(100), ncol = 10))
      colnames(df_list[[i]]) <- LETTERS[1:10]
    }
    
    # Find maximum value of all data.frames
    sapply(df_list, FUN = max)
    

    This creates a list with 10 dataframes, each with 10 observations and 10 variables. Then it loops over every data.frame to obtain the maximum value of each of those. At the end, a vector with the maximum values is returned.