简体   繁体   中英

R apply list of functions to the same set of inputs

I have many dataframes like the one below:

> dput(df)
structure(list(x1 = c(0.5, 0.65, 0.67, 0.6, 0.52, 0.47, 0.42, 
0.41, 0.4, 0.38), x2 = c(88.2759008383549, 88.4629768937181, 
88.6351264139755, 88.7928788760014, 88.9363150101317, 89.0654802835256, 
89.1805310339952, 89.2816679977014, 89.3690643397432, 89.4429508602581
), x3 = c(0.44, 0.8, 8.6, 2.72, 0.06, 0, 0, 0, 0, 0.04)), .Names = c("x1", 
"x2", "x3"), row.names = c(NA, -10L), class = "data.frame")

I want to apply a variable number of functions to each data.frame.

Here is an example with 3 functions:

library(tiger)

f1 <- function(x1,x2) lagtime(x1,x2)
f2 <- function(x1,x2) mean(x1 - x2, na.rm = TRUE)         
f3 <- function(x2,x3) sum(x2) /sum(x3)

myVector <- c( f1(df$x1,df$x2),
               f2(df$x1,df$x2),
               f3(df$x2,df$x3) )

> myVector
[1]  -6.00000 -88.44229  70.25615

However, if I have a list of n functions:

funs <- list(f1, f2, f3, ..., fn)

I should use lapply (or other function), but how?

Rewrite the functions so they take a dataframe argument and use names for the column arguments:

> f1 <- function(df, nm1 ='x1',nm2='x2') lagtime(df[[nm1]],df[[nm2]])
> f2 <- function(df, nm1 ='x1',nm2='x2') mean(df[[nm1]] - df[[nm2]], na.rm = TRUE)         
> f3 <- function(df, nm2 ='x2',nm3='x3') sum(df[[nm2]]) /sum(df[[nm3]])

Then loop over the functions and the dataframes:

 funs <- list(f1, f2, f3)
 dfs <- list(df1,df1)

 lapply(funs, function(f) sapply(dfs, function(d) f(d) ) )
#------------------
[[1]]
[1] -6 -6

[[2]]
[1] -88.44229 -88.44229

[[3]]
[1] 70.25615 70.25615

Why not put them all into one function? Here I used median in place of lagtime .

Return a list:

> foo <- function(x, y, z, ...) {
      list(median = median(c(x, y)), 
           mean = mean(x - y, ...), 
           quotient = sum(y)/sum(z))
  }

> foo(df$x1, df$x2, df$x3, na.rm = TRUE)
# $median
# [1] 44.47295
# 
# $mean
# [1] -88.44229
# 
# $quotient
# [1] 70.25615

Or as a vector:

foo2 <- function(x, y, z, ...) {
    c(median = median(c(x, y)), 
      mean = mean(x - y, ...), 
      quotient = sum(y)/sum(z))
}
foo2(df$x1, df$x2, df$x3, na.rm = TRUE)
#   median      mean  quotient 
# 44.47295 -88.44229  70.25615 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM