[英]How can I use a list of integers as the input to a function of one integer and get a list as an output?
抱歉問了一個微不足道的問題,但我很難過。 這是情況:
fishCounter(data, x, y)
其中data
是一個矩陣,x 和 y 都是整數。fishCounter
在內存中並且在我手動調用時完全正常工作(例如fishCounter(matrix(1:4,4,4), 1, 4)
)。 它的輸出是一個整數。trout
。fishCounter(trout, 3, 1:20)
。replicate
、 apply
或lapply
可以完成這項工作。lapply(c(1:19), fishCounter(trout, 3, y)
不起作用時,我感到很驚訝。我忘記了什么?
也許你可以嘗試像下面這樣的lapply
,即,
lapply(1:20, function(y) fishCounter(trout, 3, y))
或向Vectorize
你的函數fishCounter
,即,
Vectorize(fishCounter)(trout, 3, 1:20)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.