looping nested lists in R -


i have huge nested list of 15 levels. need replace empty lists occurring @ level chr "". tried lapply loop through list it's not working. there easy way this?

nested_list<-list(a=list(x=list(),y=list(i=list(),j=list(p=list(),q=list()))),b=list())  lapply(nested_list,function(x) if(length(x)==0) "" else x) 

the lapply being applied first level, how recursively loop entire nested list , perform action?

try following recursion.

foo <- function(l){     lapply(l, function(x) if(length(x)==0) "" else foo(x)) } foo(nested_list) 

edit: better version

foo <- function(l){     lapply(l, function(x) if(is.list(x) && length(x)==0) "" else if(is.list(x)) foo(x) else x) } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -