我无法找到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