rapply2d is a recursive version of lapply with two key differences to rapply: (1) Data frames are considered as final objects, not as (sub-)lists, and (2) the result is never simplified / unlisted.

rapply2d(l, FUN, ..., classes = "data.frame")

Arguments

l

a list.

FUN

a function that can be applied to all elements in l.

...

additional elements passed to FUN.

classes

character. These are classes of list-based objects inside l which FUN should be applied to. Note that FUN is also applied to all non-list elements in l. It is thus quite different from the classes argument to rapply.

Value

A list of the same structure as l, where FUN was applied to all final (atomic) elements and list-based objects of a class included in classes.

See also

Examples

l <- list(mtcars, list(mtcars, as.matrix(mtcars))) rapply2d(l, fmean)
#> [[1]] #> mpg cyl disp hp drat wt qsec #> 20.090625 6.187500 230.721875 146.687500 3.596562 3.217250 17.848750 #> vs am gear carb #> 0.437500 0.406250 3.687500 2.812500 #> #> [[2]] #> [[2]][[1]] #> mpg cyl disp hp drat wt qsec #> 20.090625 6.187500 230.721875 146.687500 3.596562 3.217250 17.848750 #> vs am gear carb #> 0.437500 0.406250 3.687500 2.812500 #> #> [[2]][[2]] #> mpg cyl disp hp drat wt qsec #> 20.090625 6.187500 230.721875 146.687500 3.596562 3.217250 17.848750 #> vs am gear carb #> 0.437500 0.406250 3.687500 2.812500 #> #>
unlist2d(rapply2d(l, fmean))
#> .id.1 .id.2 mpg cyl disp hp drat wt qsec #> 1 1 NA 20.09062 6.1875 230.7219 146.6875 3.596562 3.21725 17.84875 #> 2 2 1 20.09062 6.1875 230.7219 146.6875 3.596562 3.21725 17.84875 #> 3 2 2 20.09062 6.1875 230.7219 146.6875 3.596562 3.21725 17.84875 #> vs am gear carb #> 1 0.4375 0.40625 3.6875 2.8125 #> 2 0.4375 0.40625 3.6875 2.8125 #> 3 0.4375 0.40625 3.6875 2.8125