簡體   English   中英

lapply 與可選 function 參數和可選向量 arguments

[英]lapply with optional function argument and optional vector arguments

我在 R 中有一個 function,比如說
f1 <- 函數(x,y,vec,func0,...){
...
...
...
返回(出) }

The arguments func0 and vec in this function f1 are function object and some vector object respectively. 現在我想重復這個 function 'reps' 次(其他一切都一樣)。 我已經將這個 function 的 arguments 存儲在一個列表中,因為有很多 arguments 並且我不斷更改它們以再次進行復制。

list1 <- list(x,y,vec, func0, 其他參數)

然后我想做,f1_reps <- lapply(1:reps, f1, list1)

執行此操作時出現錯誤,因為找不到 function arguments func0 和 vec。

在這個方向上的任何幫助都會有所幫助。 這是這種情況的一個模擬示例。

這是一個例子,

a <- function(n){
  sqrt(n)
}
N = 100
out <- rep(NA,N)
# simple function with multiple arguments
foo <- function(a=a, b= c(1:3), c= 1000){
  for(n in 1:N){
    out[n] <- b%*%b+ a(n)*c
  }
  return(out)
}

candidates <- list(a=a, b = c(1:3), c=1000)
lapply(1:4, foo(a=candidates$a,b=candidates$b,c=candidates$c))  ## Doesn't work
lapply(1:4, foo, a=candidates$a, b=candidates$b, c=candidates$c) ## Doesn't work

candidates2 <- c(a=a, b = c(1:3), c=1000) # A vector of arguments
lapply(1:4, foo, a=candidates2$a, b = c(candidates2$b1,candidates2$b2,candidates2$b3), c=candidates2$c) #Doesn't work either

主要問題是您的 function f1需要輸入變量,而不是變量列表。 這是您可以使用的一種方法,例如,如果我正確理解了您的輸入是如何存儲的

# simple function with multiple arguments
foo <- function(a=1, b=2, c=3){
  return(a+b+c)
}

# works
foo(a=1, b=2, c=3)
# doesn't work as not required format
foo(list(a=1, b=2, c=3))

# formatted list such that each element has 5 elements
candidates <- list(
  a=1:5,
  b=2:6,
  c=3:7
)

# you need to apply the variables one by one with this setup
N <- 5
out <- lapply(1:N, function(i){
  foo(a=candidates$a[i]
      ,b=candidates$b[i]
      ,c=candidates$c[i])
})
out 

這使用dots又名...參數:

foo2 <- function(...) {
#I just returns the identity
  l <- lapply(..., I)
  a <- l[[1]]
  b <- l[[2]]
  c <- l[[3]]

  for(n in 1:N){
    out[n] <- b%*%b+ a(n)*c
  }
  return(out)
  }


candidates <- list(a=a, b = c(1:3), c=1000)

foo2(candidates)
# or to simplify. Same output as previous.
c(crossprod(1:3)) + sqrt(seq_len(100)) * 1000

  [1]  1014.000  1428.214  1746.051  2014.000  2250.068  2463.490  2659.751  2842.427  3014.000  3176.278  3330.625  3478.102  3619.551  3755.657
 [15]  3886.983  4014.000  4137.106  4256.641  4372.899  4486.136  4596.576  4704.416  4809.832  4912.979  5014.000  5113.020  5210.152  5305.503
 [29]  5399.165  5491.226  5581.764  5670.854  5758.563  5844.952  5930.080  6014.000  6096.763  6178.414  6258.998  6338.555  6417.124  6494.741
 [43]  6571.439  6647.250  6722.204  6796.330  6869.655  6942.203  7014.000  7085.068  7155.428  7225.103  7294.110  7362.469  7430.198  7497.315
 [57]  7563.834  7629.773  7695.146  7759.967  7824.250  7888.008  7951.254  8014.000  8076.258  8138.038  8199.353  8260.211  8320.624  8380.600
 [71]  8440.150  8499.281  8558.004  8616.325  8674.254  8731.798  8788.964  8845.761  8902.194  8958.272  9014.000  9069.385  9124.434  9179.151
 [85]  9233.544  9287.618  9341.379  9394.832  9447.981  9500.833  9553.392  9605.663  9657.651  9709.360  9760.794  9811.959  9862.858  9913.495
 [99]  9963.874 10014.000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM