How to test if list element exists?

前端 未结 7 2057
臣服心动
臣服心动 2020-11-30 21:54

Problem

I would like to test if an element of a list exists, here is an example

foo <- list(a=1)
exists(\'foo\') 
TRUE   #foo does exist
exists(\'         


        
7条回答
  •  不思量自难忘°
    2020-11-30 22:38

    A slight modified version of @salient.salamander , if one wants to check on full path, this can be used.

    Element_Exists_Check = function( full_index_path ){
      tryCatch({
        len_element = length(full_index_path)
        exists_indicator = ifelse(len_element > 0, T, F)
          return(exists_indicator)
      }, error = function(e) {
        return(F)
      })
    }
    

提交回复
热议问题