r - recursively split list elements -
r - recursively split list elements -
i cannot find right incantation of reduce
, recall
, lapply
perform next task. consider next function,
bisect.df <- function(d){ n <- ncol(d) if(n%%2) n <- n-1 # drop 1 col if odd number ind <- sample(n)[seq.int(n/2)] # split randomly both parts list(first=d[, ind], second=d[, -ind]) }
given data.frame
, returns list of 2 children data.frames
of equal ncol
extracted randomly parent. wish apply function recursively offsprings downwards given level, 3 generations. can trivially 1 generation @ time,
bisect.list <- function(l){ unlist(lapply(l, bisect.df), recursive=false) }
but how phone call recursively, n=3
times?
here's test sample play with
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))
bisect.list <- function(l,n){ for(i in 1:n) { l <- unlist(lapply(l, bisect.df), recursive=false) } return(l) }
not sure how without loop...
r
Comments
Post a Comment