簡體   English   中英

R:將match.fun用於sapply,lapply和mapply

[英]R: use of match.fun over sapply, lapply and mapply

我不確定我對match.fun的用法是否正確,不適用於這種情況。 (我不想使用for循環,因為它需要時間)。

我有一個這樣的數據框。

df <- data.frame(client = c("A", "B", "C", "D"), 
                 fun = c("bill1stEvent_OT", "bill1stEvent_Tour", "billCreation_OT", "bill1stEvent_Tour_LoadingSite"), 
                 agency = c("NA", "NA", "Agency_A", "NA"), 
                 loading_site = c("NA", "NA", "NA", "Paris"))

>df
  client                            fun    agency  loading_site
       A                bill1stEvent_OT        NA            NA
       B              bill1stEvent_Tour        NA            NA
       C                billCreation_OT  Agency_A            NA
       D  bill1stEvent_Tour_LoadingSite        NA         Paris

fun列包含我創建的函數。 每個函數采用相同的參數: clientagencyloading_site yearmonth 因此,例如,對於客戶端C ,讀取此表的方式是應用帶有參數agency = Agency_Aloading_site = NULL函數billCreation_OT 每個函數返回一個數據幀。

我有要應用功能的客戶端列表,還設置了參數yearmonth

client_list <- c("A", "C")
year <- "2018"
month <- "07"

問題:如何調用此表並僅用“一次執行”就將具有相應參數的函數應用於函數? 因此,使用上面的client_list ,我要做的是在下面運行這兩個函數,並將結果(數據幀)存儲在列表中(或任何可能的位置)。

bill1stEvent_OT(client = "A", agency = NULL, loading_site = NULL)
billCreation_OT(client = "C", agency =  Agency_A, loading_site = NULL)

我的嘗試:

fun_to_apply <- df[df$client %in% client_list, ]$fun
agency_arg <- df[df$client %in% client_list, ]$agency
loading_site_arg <- df[df$client %in% client_list, ]$loading_site

sapply(X = lapply(fun_to_apply, match.fun), 
       FUN = mapply,
       year = year,
       month = month,
       agency = agency_arg,
       loading_site = loading_site_arg)

Error in (function (client, year, month) : 
 arguments inutilisés (agency = dots[[3]][[1]], loading_site = dots[[4]][[1]])

我也不知道如何將函數參數agencyloading_siteNULL 當您讀取數據幀時,它將NA評估為類似於"NA"字符串。

謝謝你的幫助!

最終,您可以使用mapply()do.call()

D <- read.table(header=TRUE, stringsAsFactors = FALSE, text=
"fun a b
+ 3 5
- 8 2
* 4 4
+ 1 NA"
)
myfu <- function(fun, a, b) do.call(fun, list(a=a, b=b))
mapply(myfu, D$fun, D$a, D$b)

要么

unlist(Map(myfu, D$f, D$a, D$b))

為了說明我的“可以完成”評論:

# inputs
client_list <- c("A", "C")
year <- "2018"
month <- "07"

# dummy functions
bill1stEvent_OT <- function(agency, loading_site){paste("11", year, month, agency, loading_site)}
bill1stEvent_Tour <- function(agency, loading_site){paste("22", year, month, agency, loading_site)}
billCreation_OT <- function(agency, loading_site){paste("33", year, month, agency, loading_site)}
bill1stEvent_Tour_LoadingSite <- function(agency, loading_site){paste("44", year, month, agency, loading_site)}


# loop through rows, match function name
apply(df, 1, function(i){
  # we can add a step to convert "NA" to actual NA.
  if(i[1] %in% client_list){
         match.fun(i[2])(agency = i[3], loading_site = i[4])
    } else {NA}
})

# [1] "11 2018 07 NA NA"       NA                       "33 2018 07 Agency_A NA" NA                      

顯示了可能的方法后,我不建議您使用此方法。 如果您可以告訴我們更多有關此問題的信息,則有更好的方法。

我創建了一些示例函數來打印參數:

bill1stEvent_OT <- function(...) {
  print("bill1stEvent_OT")
  lapply(list(...), print)
  cat("\n")
}

billCreation_OT <- function(...) {
  print("bill1stEvent_OT")
  lapply(list(...), print)
  cat("\n")
}

我認為您正在尋找的是類似的東西(get等同於match.fun):

apply(df[df$client %in% client_list,], 1, 
      function(x) get(x["fun"])(x["agency"], x["loading_site"]))
[1] "bill1stEvent_OT"
agency 
  "NA" 
loading_site 
        "NA" 

[1] "bill1stEvent_OT"
    agency 
"Agency_A" 
loading_site 
        "NA" 

NULL

暫無
暫無

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

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