Given a nested list for example as below
lst <- list(
1,
list(list(c(4, 5, 4)), list(c(6, 7))),
list(c(2, 3, 3)),
list(list(c(5, 5, 6)), list(c(7, 7, 7)))
)
> str(lst)
List of 4
$ : num 1
$ :List of 2
..$ :List of 1
.. ..$ : num [1:3] 4 5 4
..$ :List of 1
.. ..$ : num [1:2] 6 7
$ :List of 1
..$ : num [1:3] 2 3 3
$ :List of 2
..$ :List of 1
.. ..$ : num [1:3] 5 5 6
..$ :List of 1
.. ..$ : num [1:3] 7 7 7
Let's say its deepest level is 3, e.g., depths of vectors 4 5 4, 6 7, 5 5 6 and 7 7 7 in lst.
I am wondering if there is a way that only runs a certain function over those deepest levels, while other levels are untouched. For example, if the the function is unique, then my expected output is
lstout <- list(
1,
list(list(c(4, 5)),list(c(6,7))),
list(c(2, 3, 3)),
list(list(c(5, 6)), list(7))
)
> str(lstout)
List of 4
$ : num 1
$ :List of 2
..$ :List of 1
.. ..$ : num [1:2] 4 5
..$ :List of 1
.. ..$ : num [1:2] 6 7
$ :List of 1
..$ : num [1:3] 2 3 3
$ :List of 2
..$ :List of 1
.. ..$ : num [1:2] 5 6
..$ :List of 1
.. ..$ : num 7
It seems rapply cannot run the function selectively only on the deepest level. I have no clue how to make it.
Any base R idea or solution would be greatly appreciated!