Search code examples
rfunctionapplylapplymapply

Apply a multi parameter function to all data frames in a list in R


I have a list of data frames each with 3 columns.

list_of_df <- list(df1 = data.frame(f = c(6,4,2,9,7), g = c(7,5,3,1,8), h = c(4,2,1,3,6)),
                   df2 = data.frame(f = c(5,3,1,8), g = c(6,4,2,9), h = c(4,1,5,7)))
list_of_df
$df1
  f g h
1 6 7 4
2 4 5 2
3 2 3 1
4 9 1 3
5 7 8 6

$df2
  f g h
1 5 6 4
2 3 4 1
3 1 2 5
4 8 9 7

etc.

I have defined a function to calculate the area under the curve (auc):

auc <- function(x,y) sum(diff(x) * (head(y,-1)+tail(y,-1)))/2

For each data frame, column f is the input x and column g is the input y.

Now I want to use a version of apply() (not fussed which one) to apply the function auc to each data frame in the list. But I need to be able to specify that columns f and g should be used as the input.

So far, all my attempts to specify the columns have instead resulted in accidentally specifying a data frame from the list.

Any ideas how I might be able to do this? Thanks.


Solution

  • Your AUC function does not seem right to me, but the sapply function takes a list as an input.

    auc <- function(x,y) sum(diff(x) * (head(y,-1)+tail(y,-1)))/2
    
    list_of_df <- list(df1 = data.frame(f = c(6,4,2,9,7), g = c(7,5,3,1,8), h = c(4,2,1,3,6)),
                       df2 = data.frame(f = c(5,3,1,8), g = c(6,4,2,9), h = c(4,1,5,7)))
    list_of_df
    $df1
      f g h
    1 6 7 4
    2 4 5 2
    3 2 3 1
    4 9 1 3
    5 7 8 6
    
    $df2
      f g h
    1 5 6 4
    2 3 4 1
    3 1 2 5
    4 8 9 7
    
    sapply(list_of_df, function(x) auc(x$f, x$g))
      df1   df2 
    -15.0  22.5