# Example data
## Renewable energy shares per country (% of total consumption) in 2016
data("renewable_energy_by_country")
## Renewable energy shares in Oceania
renewable_oceania <- renewable_energy_by_country[["World"]]["Oceania"]
## Pokemon properties in Pokemon GO
data("pokedex")
# List pruning and unnesting
## Drop logical NA's while preserving list structure
na_drop_oceania <- rrapply(
renewable_oceania,
f = function(x) x,
classes = "numeric",
how = "prune"
)
str(na_drop_oceania, list.len = 3, give.attr = FALSE)
## Drop logical NA's and return unnested list
na_drop_oceania2 <- rrapply(
renewable_oceania,
classes = "numeric",
how = "flatten"
)
head(na_drop_oceania2, n = 10)
## Flatten to simple list with full names
na_drop_oceania3 <- rrapply(
renewable_oceania,
classes = "numeric",
how = "flatten",
options = list(namesep = ".", simplify = FALSE)
)
str(na_drop_oceania3, list.len = 10, give.attr = FALSE)
## Drop logical NA's and return melted data.frame
na_drop_oceania4 <- rrapply(
renewable_oceania,
classes = "numeric",
how = "melt"
)
head(na_drop_oceania4)
## Reconstruct nested list from melted data.frame
na_drop_oceania5 <- rrapply(
na_drop_oceania4,
how = "unmelt"
)
str(na_drop_oceania5, list.len = 3, give.attr = FALSE)
## Unnest list to wide data.frame
pokedex_wide <- rrapply(pokedex, how = "bind")
head(pokedex_wide)
## Unnest to data.frame including parent columns
pokemon_evolutions <- rrapply(
pokedex,
how = "bind",
options = list(namecols = TRUE, coldepth = 5)
)
head(pokemon_evolutions, n = 10)
# Condition function
## Drop all NA elements using condition function
na_drop_oceania6 <- rrapply(
renewable_oceania,
condition = Negate(is.na),
how = "prune"
)
str(na_drop_oceania6, list.len = 3, give.attr = FALSE)
## Replace NA elements by a new value via the ... argument
## NB: the 'newvalue' argument should be present as function
## argument in both 'f' and 'condition', even if unused.
na_zero_oceania <- rrapply(
renewable_oceania,
condition = function(x, newvalue) is.na(x),
f = function(x, newvalue) newvalue,
newvalue = 0,
how = "replace"
)
str(na_zero_oceania, list.len = 3, give.attr = FALSE)
## Filter all countries with values above 85%
renewable_energy_above_85 <- rrapply(
renewable_energy_by_country,
condition = function(x) x > 85,
how = "prune"
)
str(renewable_energy_above_85, give.attr = FALSE)
# Special arguments .xname, .xpos, .xparents and .xsiblings
## Apply a function using the name of the node
renewable_oceania_text <- rrapply(
renewable_oceania,
condition = Negate(is.na),
f = function(x, .xname) sprintf("Renewable energy in %s: %.2f%%", .xname, x),
how = "flatten"
)
head(renewable_oceania_text, n = 10)
## Extract values based on country names
renewable_benelux <- rrapply(
renewable_energy_by_country,
condition = function(x, .xname) .xname %in% c("Belgium", "Netherlands", "Luxembourg"),
how = "prune"
)
str(renewable_benelux, give.attr = FALSE)
## Filter European countries with value above 50%
renewable_europe_above_50 <- rrapply(
renewable_energy_by_country,
condition = function(x, .xpos) identical(.xpos[c(1, 2)], c(1L, 5L)) & x > 50,
how = "prune"
)
str(renewable_europe_above_50, give.attr = FALSE)
## Filter European countries with value above 50%
renewable_europe_above_50 <- rrapply(
renewable_energy_by_country,
condition = function(x, .xparents) "Europe" %in% .xparents & x > 50,
how = "prune"
)
str(renewable_europe_above_50, give.attr = FALSE)
## Return position of Sweden in list
(xpos_sweden <- rrapply(
renewable_energy_by_country,
condition = function(x, .xname) identical(.xname, "Sweden"),
f = function(x, .xpos) .xpos,
how = "flatten"
))
renewable_energy_by_country[[xpos_sweden$Sweden]]
## Return neighbors of Sweden in list
siblings_sweden <- rrapply(
renewable_energy_by_country,
condition = function(x, .xsiblings) "Sweden" %in% names(.xsiblings),
how = "flatten"
)
head(siblings_sweden, n = 10)
## Unnest selected columns in Pokedex list
pokedex_small <- rrapply(
pokedex,
condition = function(x, .xpos, .xname) length(.xpos) < 4 & .xname %in% c("num", "name", "type"),
how = "bind"
)
head(pokedex_small)
# Modifying list elements
## Calculate mean value of Europe
rrapply(
renewable_energy_by_country,
condition = function(x, .xname) .xname == "Europe",
f = function(x) mean(unlist(x), na.rm = TRUE),
classes = "list",
how = "flatten"
)
## Calculate mean value for each continent
## (Antarctica's value is missing)
renewable_continent_summary <- rrapply(
renewable_energy_by_country,
condition = function(x, .xpos) length(.xpos) == 2,
f = function(x) mean(unlist(x), na.rm = TRUE),
classes = "list"
)
str(renewable_continent_summary, give.attr = FALSE)
## Filter country or region by M49-code
rrapply(
renewable_energy_by_country,
condition = function(x) attr(x, "M49-code") == "155",
f = function(x, .xname) .xname,
classes = c("list", "ANY"),
how = "unlist"
)
# Recursive list updating
## Recursively remove list attributes
renewable_no_attrs <- rrapply(
renewable_oceania,
f = function(x) c(x),
classes = c("list", "ANY"),
how = "recurse"
)
str(renewable_no_attrs, list.len = 3, give.attr = TRUE)
## recursively replace all names by M49-codes
renewable_m49_names <- rrapply(
renewable_oceania,
f = function(x) attr(x, "M49-code"),
how = "names"
)
str(renewable_m49_names, list.len = 3, give.attr = FALSE)
# List attributes
## how = "list" preserves all list attributes
na_drop_oceania_attr <- rrapply(
renewable_oceania,
f = function(x) replace(x, is.na(x), 0),
how = "list"
)
str(na_drop_oceania_attr, max.level = 2)
## how = "prune" also preserves list attributes
na_drop_oceania_attr2 <- rrapply(
renewable_oceania,
condition = Negate(is.na),
how = "prune"
)
str(na_drop_oceania_attr2, max.level = 2)
# Expressions
## Replace logicals by integers
call_old <- quote(y <- x <- 1 + TRUE)
call_new <- rrapply(call_old,
f = as.numeric,
how = "replace",
classes = "logical"
)
str(call_new)
## Update and decompose call object
call_ast <- rrapply(call_old,
f = function(x) ifelse(is.logical(x), as.numeric(x), x),
how = "list"
)
str(call_ast)
## Prune and decompose expression
expr <- expression(y <- x <- 1, f(g(2 * pi)))
is_new_name <- function(x) !exists(as.character(x), envir = baseenv())
expr_prune <- rrapply(expr,
classes = "name",
condition = is_new_name,
how = "prune"
)
str(expr_prune)
## Prune and flatten expression
expr_flatten <- rrapply(expr,
classes = "name",
condition = is_new_name,
how = "flatten"
)
str(expr_flatten)
## Prune and melt expression
rrapply(expr,
classes = "name",
condition = is_new_name,
f = as.character,
how = "melt"
)
## Avoid recursing into call objects
rrapply(
expr,
classes = "language",
condition = function(x) !any(sapply(x, is.call)),
how = "flatten"
)
Run the code above in your browser using DataLab