Skip to contents

Recursively apply a function to each element of a nested list

Usage

recurse_nested(x, func)

Arguments

x

The element on which to operate, which could be a list or some other type of object.

func

The function to apply to each element of the list.

Value

A list which is the same as the list provided but with the given function applied to each element.

Examples

nested <- list(
  a = "1",
  b = "12.5",
  c = "NA",
  d = "alphabet",
  e = list(
    first = "NA",
    second = "notanumber",
    third = "100",
    fourth = "0.45",
    fifth = list(
      one = "teal",
      two = "NA",
      three = "999"
    )
  )
)
recurse_nested(nested, parse_string)
#> $a
#> [1] 1
#> 
#> $b
#> [1] 12.5
#> 
#> $c
#> [1] NA
#> 
#> $d
#> [1] "alphabet"
#> 
#> $e
#> $e$first
#> [1] NA
#> 
#> $e$second
#> [1] "notanumber"
#> 
#> $e$third
#> [1] 100
#> 
#> $e$fourth
#> [1] 0.45
#> 
#> $e$fifth
#> $e$fifth$one
#> [1] "teal"
#> 
#> $e$fifth$two
#> [1] NA
#> 
#> $e$fifth$three
#> [1] 999
#> 
#> 
#>