[R] recursive lapply and keeping track of data
Giovanni Gherdovich
g.gherdovich at gmail.com
Tue Aug 15 07:22:41 CEST 2017
Hello William,
that's exactly what I needed. I didn't consider lapply'ing over
seq_along(data) instead of data, very useful.
Thanks a lot!
Giovanni
On Mon, Aug 14, 2017 at 10:02 PM, William Dunlap <wdunlap at tibco.com> wrote:
> You could replace your 'depth' argument with one that shows where in the
> original data you are at:
>
> leaf.func <-
> function(data, where) {
> if(is.null(data)) stop("Null data at ", deparse(where))
> return(mean(data))
> }
> visit.level <-
> function(data, where = integer()) {
> if (length(where) == 2) {
> return(leaf.func(data, where))
> } else {
> res <- lapply(seq_along(data), function(i)
> visit.level(data[[i]], where = c(where, i)))
> names(res) <- names(data)
> return(res)
> }
> }
>
> E.g.,
>> new.data <- visit.level(data, integer())
> Error in leaf.func(data, where) : Null data at c(3L, 2L)
>> data[[3]][2]
> $Madrid
> NULL
More information about the R-help
mailing list