A little recursive function can do this for you:
depth <- function(this,thisdepth=0){
if(!is.list(this)){
return(thisdepth)
}else{
return(max(unlist(lapply(this,depth,thisdepth=thisdepth+1))))
}
}
If you've got package:testthat
, here's a test set:
l1=list(1,2,3)
l2=list(1,2,l1,4)
l3=list(1,l1,l2,5)
require(testthat)
expect_equal(depth(l1),1)
expect_equal(depth(l2),2)
expect_equal(depth(l3),3)
Apologies for using lower-case L in variable names. Readability fail.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…