我正在嘗試從資料表中檢索一個值作為更大的自定義函式的一部分。我可以生成行號,但無法從該行中檢索值。該公式在函式環境之外有效,但在內部無效。
example_outlier_table <- data.table(dataset = c("doe", "ray", "me", "fa", "so"),
upper_limit = c(2,6,9,11,7))
example_function <- function(dt,otable){
return(match(deparse(substitute(dt)), otable$dataset))
}
example_function(ray, example_outlier_table)
結果 = 2
這是正確的,“射線”是“資料集”列中的第二個條目
在此示例中,“ray”既是“example_outlier_table$dataset”中的字串,也是另一個資料表物件的名稱,因此是“deparse(substitute(dt))”步驟。
問題是這樣的:我想在我的自定義函式中的另一個位置使用 'ray' 在 example_outlier_table 編號 6 中指示的值。
example_function <- function(dt,otable){
return(otable[dataset == as.character(deparse(substitute(dt))),
upper_limit])
}
example_function(ray, example_outlier_table)
結果 = 數字(0)不正確
example_function <- function(dt,otable){
return(otable[match(deparse(substitute(dt)), otable$dataset),
upper_limit])
}
example_function(ray, example_outlier_table)
結果 = [1] 不適用
uj5u.com熱心網友回復:
我們可以直接提取列[[
example_function <- function(dt,otable){
dt <- deparse(substitute(dt))
otable[["upper_limit"]][otable[["dataset"]] == dt]
}
-測驗
example_function(ray, example_outlier_table)
[1] 6
或者使用data.table方法
example_function <- function(dt,otable){
dt <- deparse(substitute(dt))
otable[dataset == dt, upper_limit][[1]]
}
example_function(ray, example_outlier_table)
[1] 6
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/446299.html
上一篇:如何檢查R中資料的凸性?
