Home > Back-end >  In R--Change name of list of list of lists when lists have differing lengths
In R--Change name of list of list of lists when lists have differing lengths

Time:08-20

I've tried everything I can...I have a large list of lists. They are of varying depths, but they all have a variable name that I need to rename. I tried breaking apart the list into data frames, it just seems unpractical and doesn't even do what I want.

Here's a toy example:

list1 = list(changethis = c("1", "2"))
list2 = list(varname1 = c("1,2,3,4"), changethis = c("5,6,7,8"), varname2 = c("9, 10, 11"))
list3 = list(varname3 = list(varname4 = c("first", "second", "third", list(changethis = c("15, 16, 19"), varname5 = "cat", "dog", "fish"))))
list4 = list(varname6 = list(varname7 = list2, varname8 = list2))
list5a = list(varname13 = c("hat", "key"), changethis = c("5"))
list5 = list(varname9 = list(varname10 = list5a, varname11 = list5a)) 
list6 = list(varname12 = list5)
list7 = list(first = list1)

listbig = list(sublist1 = list3, sublist2 = list4, sublist3 = list5, sublist4= list6, sublist5=list7, sublist6 = list5a)

Here's a toy code that produces what I want it to look like. The 'changethis' var is renamed to 'change'

sollist1 = list(changed= c("1", "2"))
sollist2 = list(varname1 = c("1,2,3,4"), changed = c("5,6,7,8"), varname2 = c("9, 10, 11"))
sollist3 = list(varname3 = list(varname4 = c("first", "second", "third", list(changed = c("15, 16, 19"), varname5 = "cat", "dog", "fish"))))
sollist4 = list(varname6 = list(varname7 = sollist2, varname8 = sollist2))
sollist5a = list(varname13 = c("hat", "key"), changed = c("5"))
sollist5 = list(varname9 = list(varname10 = sollist7, varname11 = sollist5a)) 
sollist6 = list(varname12 = sollist5)
sollist7 = list(first = sollist1)
solution_list = list(sublist1 = sollist3, sublist2 = sollist4, sublist3 = sollist5, sublist4= sollist6, sublist5=sollist7, sublist6=sollist7)

Here is one of my many attempts to do this. I extracted sublist1 from the big list and tried to just change the name for it, but nothing gets changed.

extr_sublist1 <- listbig[1]
names(extr_sublist1[[1]][[1]][[1]][4]) <- "changed" #does not do it...

Another failed attempt: In this I extract a differently hierarchial sublist and create a Var name which I hope to loop over so I can change the name of the 'changethis' var. Also does not work.

extr_sublist4 <- listbig[4]
numvars <- length(extr_sublist4[[1]][[1]][[1]])

for (i in 1: numvars){
  varname<-paste("Var",numvar, sep = "")
  paste('Var',[i]) <- extr_sublist4[[1]][[1]][[1]][[1]][2]
  namespaste('Var',[i])[paste('Var',[i]) == 'changethis'] <- 'changed'
}

I'm sure there's a simple and elegant solution to this...but have no idea what. Thanks in advance for your help.

CodePudding user response:

You can use a recursive method to do the transformation:

changefun <- function(x, change_name, new_name){
  idx <- names(x) == change_name
  if(any(idx)) names(x)[idx] <- new_name
  if (is.list(x)) lapply(x, changefun, change_name, new_name)
  else x
}

Now just call

changefun(listbig, 'changethis', 'changed')
  • Related