我無法找到Reduce
,Recall
,lapply
的正確咒語來執行以下任務。考慮下面的函數,遞歸分割列表元素
bisect.df <- function(d){
n <- ncol(d)
if(n%%2) n <- n-1 # drop one col if odd number
ind <- sample(n)[seq.int(n/2)] # split randomly both parts
list(first=d[, ind],
second=d[, -ind])
}
給予data.frame
,它返回兩個孩子從他們的父母隨機抽取同等ncol
的data.frames
的列表。我希望將這個函數遞歸地應用到後代,直到一個給定的水平,比如3代。我可以一次平凡的一代人做,
bisect.list <- function(l){
unlist(lapply(l, bisect.df), recursive=FALSE)
}
但我怎麼遞歸調用這個,說N=3
次?
這是一個測試樣品與
d <- data.frame(matrix(rnorm(16*5), ncol=16))
step1 <- bisect.list(list(d))
step2 <- bisect.list(step1)
step3 <- bisect.list(step2)
str(list(step1, step2, step3))
我的意思是說沒有'for'循環,爲了調味起來。但是你得到+1是因爲它完成了工作:) – baptiste 2012-02-02 00:00:32
我會去for循環,因爲它是最容易閱讀的。 – baptiste 2012-02-02 01:41:33