Applications

  • Reorder a list by it’s names attribute
  • (Exercise 5-14)
x <- list(a=1, b=2)
y <- list(c=3, a=4)

union_list <- function(x,y){
# combine lists
z <- c(x,y)
# order by names (increasing) then by value (decreasing)
z <- z[order(names(z), unlist(z), 
             decreasing = c(FALSE, TRUE),
             method="radix")]
# remove duplicated names
z[!(duplicated(names(z)) == TRUE)]
}

str(union_list(x,y))
## List of 3
##  $ a: num 4
##  $ b: num 2
##  $ c: num 3