Apply over nested list names: Sub out character in nested list names

Here is an idea for a recursive function. It first substitutes the periods in the names with underscores. It then checks if the class of an element is list, and if yes, it applies the function on that element. Otherwise, if the class is character, it substitutes the periods in its elements with underscores. Note that this will not work if there are for example data.frames in the list, that would have to be an extension defined in the function as well. Hope this helps!

Function:

my_func <- function(x)
{
    names(x) <- gsub('\\.','_',names(x) )
    for(i in 1:length(x))
    {
      if(any(class(x[[i]])=='list'))
      {
        x[[i]] <- my_func(x[[i]])
      }
    }
    return(x)
}
y <- my_func(x)

Data:

x <- list(
  urban = list(
    cars = c('volvo', 'ford'),
    food.dining = list(
      local.business = c('carls'),
      chain.business = c('dennys', 'panera')
    )
  ),
  rural = list(
    land.use = list(
      farming =list(
        dairy = c('cows'),
        vegie.plan = c('carrots')
      )
    ),
    social.rec = list(
      community.center = c('town.square')
    ),
    people.type = c('good', 'bad', 'in.between')
  ),
  other.locales = c('suburban'),
  missing = list(
    unknown = c(),
    known = c()
  ),
  end = c('wow')
)

Output:

 str(y)

List of 5
 $ urban        :List of 2
  ..$ cars       : chr [1:2] "volvo" "ford"
  ..$ food_dining:List of 2
  .. ..$ local_business: chr "carls"
  .. ..$ chain_business: chr [1:2] "dennys" "panera"
 $ rural        :List of 3
  ..$ land_use   :List of 1
  .. ..$ farming:List of 2
  .. .. ..$ dairy     : chr "cows"
  .. .. ..$ vegie_plan: chr "carrots"
  ..$ social_rec :List of 1
  .. ..$ community_center: chr "town.square"
  ..$ people_type: chr [1:3] "good" "bad" "in.between"
 $ other_locales: chr "suburban"
 $ missing      :List of 2
  ..$ unknown: NULL
  ..$ known  : NULL
 $ end          : chr "wow"

For list objects, it will rename the list and recursively call the same function for each of its elements. For character objects, it will just return the character.

library('purrr')

fix_names.list <- function(v) {
  names(v) <- gsub('\\.', '_', names(v))
  map(v, fix_names)
}

fix_names.default <- function(v) v

fix_names <- function(v) UseMethod('fix_names')

fix_names(x) %>% str
 # List of 5
 # $ urban        :List of 2
 #  ..$ cars       : chr [1:2] "volvo" "ford"
 #  ..$ food_dining:List of 2
 #  .. ..$ local_business: chr "carls"
 #  .. ..$ chain_business: chr [1:2] "dennys" "panera"
 # $ rural        :List of 3
 #  ..$ land_use   :List of 1
 #  .. ..$ farming:List of 2
 #  .. .. ..$ dairy     : chr "cows"
 #  .. .. ..$ vegie_plan: chr "carrots"
 #  ..$ social_rec :List of 1
 #  .. ..$ community_center: chr "town.square"
 #  ..$ people_type: chr [1:3] "good" "bad" "in.between"
 # $ other_locales: chr "suburban"
 # $ missing      :List of 2
 #  ..$ unknown: NULL
 #  ..$ known  : NULL
 # $ end          : chr "wow"

Tags:

List

Recursion

R