我是 R 的新手,我試圖在 R 中定義一個函式,我在其中呼叫 R 包(pgls 和 sma)中已有的另一個函式。我不確定該怎么做,甚至不知道是否可能。
我嘗試了以下方法:
對于 pgls
getpgls <- function(P1, P2, dataf){
PGLSt <- pgls(log(P1)~log(P2), data = dataf, lambda = 'ML')
}
當我呼叫函式時:
getpgls(sym('Long'), sym('massAvg'), CompData)
我得到:
Error in log(P1) : non-numeric argument to mathematical function
sma 函式也會發生類似的情況:
getsma <- function(P1, P2, dataf){
SMAt <- sma(P1~P2,
log = "xy",
data = dataf,
)
}
當我呼叫函式時:
getsma(sym('Long'), sym('massAvg'), Data_Animal_de_pd)
我收到以下錯誤:
Error in model.frame.default(formula = P1 ~ P2, data = dataf, drop.unused.levels = TRUE) :
object is not a matrix
當我使用相同的引數運行 pgls 和 sma 時,但在函式之外,它運行得很好。IE。
Long.SMA <- sma(Long~massAvg,
log = "xy",
data = Data_Animal_de_pd,
)
和
Long.PGLS = pgls(log(Long)~log(massAvg), data = CompData, lambda = 'ML')
編輯:這里我包括了 CompData 和 Data_Animal_de_pd 的小版本(只有 10 只動物和引數 massAvg 和 Long)。
CompData 的類是“comparative.data”,來自函式 comparative.data,它將系統發育樹與另一個資料框 (Data_Animal_de_pd) 連接起來。
> dput(CompData)
structure(list(phy = structure(list(edge = structure(c(11L, 12L,
13L, 14L, 14L, 15L, 15L, 16L, 17L, 17L, 16L, 13L, 12L, 18L, 18L,
11L, 19L, 19L, 12L, 13L, 14L, 1L, 15L, 2L, 16L, 17L, 3L, 4L,
5L, 6L, 18L, 7L, 8L, 19L, 9L, 10L), dim = c(18L, 2L)), edge.length = c(100.597661,
5.254328, 4.311278, 71.0845800943, 34.327960646, 36.7566030561,
5.779375747, 15.0619109945, 15.9153248095, 15.9153245794, 30.9772360366,
75.39586827, 44.21113726, 36.439042146, 36.4390420969, 108.977279909,
72.27059073, 72.270578302), Nnode = 9L, tip.label = c("Tupaia_minor",
"Hystrix_cristata", "Geocapromys_brownii", "Myocastor_coypus",
"Hydrochoerus_hydrochaeris", "Rhinoceros_sondaicus", "Dasypus_hybridus",
"Tolypeutes_matacus", "Caluromysiops_irrupta", "Acrobates_pygmaeus"
), node.label = 11:19), class = "phylo", order = "cladewise"),
data = structure(list(massAvg = c(0.045, 20, 1.5, 7.5, 50.5,
1350, 5.5, 1.5, 0.45, 0.01), Long = c(21.565, 110.4, 55.52,
68.3266666666667, 175.2, 447.4, 47.02, 44.68, 38.58, 12.67
)), row.names = c("Tupaia_minor", "Hystrix_cristata", "Geocapromys_brownii",
"Myocastor_coypus", "Hydrochoerus_hydrochaeris", "Rhinoceros_sondaicus",
"Dasypus_hybridus", "Tolypeutes_matacus", "Caluromysiops_irrupta",
"Acrobates_pygmaeus"), class = "data.frame"), data.name = "datanm2[, c(\"massAvg\", \"Long\", \"Sci_name2\")]",
phy.name = "newphy", dropped = list(tips = character(0),
unmatched.rows = character(0))), class = "comparative.data")
Data_Animal_de_pd是一個資料框,包含了動物的資訊,比如骨骼的長度等。
> dput(Data_Animal_de_pd)
structure(list(massAvg = c(20, 50.5, 7.5, 1350, 0.45, 0.045,
1.5, 5.5, 1.5, 0.01), Long = c(110.4, 175.2, 68.3266666666667,
447.4, 38.58, 21.565, 55.52, 47.02, 44.68, 12.67), Sci_name = c("Hystrix cristata",
"Hydrochoerus hydrochaeris", "Myocastor coypus", "Rhinoceros sondaicus",
"Caluromysiops irrupta", "Tupaia minor", "Geocapromys brownii",
"Dasypus hybridus", "Tolypeutes matacus", "Acrobates pygmaeus"
), Sci_name2 = c("Hystrix_cristata", "Hydrochoerus_hydrochaeris",
"Myocastor_coypus", "Rhinoceros_sondaicus", "Caluromysiops_irrupta",
"Tupaia_minor", "Geocapromys_brownii", "Dasypus_hybridus", "Tolypeutes_matacus",
"Acrobates_pygmaeus")), row.names = c("10137", "10149", "10157",
"102233", "126286", "143289", "1543402", "1756220", "183749",
"190720"), class = "data.frame")```
uj5u.com熱心網友回復:
要使您的函式使用符號(我假設來自 rlang::sym),您必須使用 rlang::inject 注入它們:
getsma <- function(P1, P2, dataf){
SMAt <- rlang::inject(sma(!!P1 ~ !!P2,
log = "xy",
data = dataf,
))
}
但您可以替代并注入引數:
getsma <- function(P1, P2, dataf){
P1 <- rlang::enexpr(P1)
P2 <- rlang::enexpr(P2)
SMAt <- rlang::inject(sma(!!P1 ~ !!P2,
log = "xy",
data = dataf,
))
}
然后直接打電話給他們:
getsma(Long, massAvg, Data_Animal_de_pd)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/537602.html
標籤:r功能