
$$$$$$$$$$$$$$$$$$$$$$$$$$$$
ORIGINAL DO NOT TOUCH
WORKS IF THERE ARE NO NA VALUES

getElemsR <- function(dat, Index, Lev=2)  {
  if ((Lev <- (Lev - 1)) > 1)  {
    inds <- seq(length(dat[[1]]))
    dat <- unlist(lapply(inds, function(i) getElemsR(dat, i, Lev)), recursive=F)
  } 
  sapply(dat, "[[", Index, USE.NAMES=T, simplify=F)
}

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$


#######$$$$$$$$$$$$$$
## WORKING HERE   ###
######################
dat <- deepLevsNA[[1]];  Index <- 3;  Lev <- 4

getElemsR <- function(dat, Index, Lev=2)  {

  # Check for 'empty' list
  if (is.na(dat) || is.null(dat) || length(dat)==0) 
      return(NA)

  if (Lev  > 2L)  {
    catLogging.lengths()

    # create index for for-loop
    inds <- seq(length(dat))

    lapply(inds, function(i) ifelse(all(is.na(dat[[i]])), NA, dat[[i]]))

    # ultimate goal.  to check if NA, MISSING, etc
 #$    levIndx <- lapply(inds, function(i) c(i, rep(1L, Lev-2L)))
 #$    NAS <- sapply(levIndx, function(i) (is.na(dat[[i]])))
 #$
 #$   getElemsR(
 #$     dat=lapply(inds, function(i) ifelse(all(NAS[[i]]), NA, dat[[i]]))
 #$     Index=Index
 #$     Lev=Lev-1L
 #$     )

    dat <- unlist(lapply(inds, function(i) 
      if (is.na(dat[i]))
        {cat("NA for: i=", i, "\n"); str(dat[i])} else         {cat("!NA for: i=", i, "\n"); str(dat[i])})
      , recursive=F)
  } 
  sapply(dat, "[[", Index, USE.NAMES=T, simplify=F)
}

## TESTING
getElemsR(deepLevs, 3, 5)
getElemsR(deepLevsNA, 3, 5)





#######$$$$$$$$$$$$$$

getElemsR <- function(dat, Index, Lev=2)  {
  if ((Lev <- (Lev - 1)) > 1)  {
    inds <- seq(length(dat))
    dat <- unlist(lapply(inds, function(i) 
      ifelse( is.na(dat[i]), NA, getElemsR(dat, i, Lev)))
      , recursive=F)
  } 

  inds <- 1:length(dat)
  sapply(inds, function(i) 
      ifelse( is.na(dat[Index]), NA, dat[[Index]])
#  sapply(dat, "[[", Index, USE.NAMES=T, simplify=F)
  , simplify=F
  )
}


#######$$$$$$$$$$$$$$


getElemsR <- function(dat, Index, Lev=2)  {
  if ((Lev <- (Lev - 1)) > 1)  {
    inds <- seq(length(dat))
     dat <- unlist(lapply(inds, function(i) {cat("i is=", i, "\n");

ifelse( all(is.na(dat[i])), NA, getElemsR(dat, i, Lev))

#      getElemsR(dat, i, Lev)

      } )
      , recursive=F)
  } 
  sapply(dat, "[[", Index, USE.NAMES=T, simplify=F)
}




#######$$$$$$$$$$$$$$

getElemsR <- function(dat, Index, Lev=2)  {
  if ((Lev <- (Lev - 1)) > 1)  {
    
    # make sequence of 1:(size of longest list)
    inds <- 1:max(sapply(dat, length))

    inds <- 1:length(dat)
    dat <- unlist(lapply(inds, function(i) 
                    ifelse( is.na(dat[i]), NA, getElemsR(dat, i, Lev))), 
                 recursive=F)
  } 
  sapply(dat, "[[", Index, USE.NAMES=T, simplify=F)
}

# get the 2nd element of every 2nd-Level list
getElemsR(myData, 3)  # Lev=2 by default

# get the 2nd element of every 3rd-Level list
getElemsR(myData, 2, 3)

# if needed as a vector:
unlist(getElemsR(myData, 2, 3))


data1 <- lapply(1:3, paste0, c("A","B"))
data2 <- lapply(4:6, paste0, c("A","B"))

dataWithNA <- list(list("EA", "EB"), NA, list("GA", "GB"))

names(data1) <- names(data2) <- c("_sub1", "_sub2", "_sub3")
myData <- list(Lev1A=data1, Lev1B=dataWithNA, Lev1C=data2)

myData 
paste0("_",1:3)

myData <- list(Lev1A=list("1Aa"=data1, "1Ab"=data2), Lev1B=list("`1Ba"=data2, "1Bb"=data2), Lev1C=list("1Ca"=data1, "1Cb"=data1), Lev1D=list("1Da"=data1, "1Db"=data2))



lapply(inds, function(i) {
cat("

  i is ", i, "
  ")
str(dat[[i]])
print(all(is.na(dat[[i]])))
print(is.na(dat[i]))

                    ifelse( is.na(dat[i]), NA, getElemsR(dat, i, Lev))
})









#$$$$$$$$$$$$$$$$$$$$$$$$$$
#$$$$$$$$$$$$$$$$$$$$$$$$$$

## LOGGING
catLogging.lengths <- function()     cat("      Len(dat) is: ", length(dat), " 
      Len(dat[1]) is", length(dat[1]), " 
      Len(dat[[1]]) is", length(dat[[1]]), "

      ")



#$$$$$$$$$$$$$$$$$$$$$$$$$$

A SLOPPY ATTEMPT TO CREATE DEEP LEVELS;   


  
A  B   C   D   E   F 

         1           2
   1         2              4    
1    2    3    4          7    8

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
# THIS KIND OF WORKS
        # Parameter to pass in
        levelsDeep <- 4
        lngOfFinalLevel <- 3

        # Initiate list
        levs <- as.list(rep(NA, levelsDeep))
        names(levs) <- paste0("Lev", seq(levelsDeep))

        # Create Leaves
        suffix <- LETTERS[1:lngOfFinalLevel]
        leaves <- lapply(seq(2^(levelsDeep-1)), function(i) paste0(i, suffix))

        # group into final level

        levs[[levelsDeep]] <- leaves
        for (j in (levelsDeep-1):1) {
          names(levs[[j+1]]) <- paste0("L", j+1, "_", seq(levs[[j+1]]))
          levs[[j]] <- lapply(seq(2^(j-1)), function(i) list(levs[[ c(j+1, 2*i-1) ]], levs[[ c(j+1, 2*i) ]]) )
        }

        # Clean up names to Level 1  ( TODO: this is sloppy and needs fixing)
        j <- 0
        names(levs[[j+1]]) <- paste0("L", j+1, "_", seq(levs[[j+1]]))

        ## FINAL DATA
        deepLevs <- levs[[1]]
        bottom <- levs[[levelsDeep]]

        ## CREATE DATA WITH NA
        deepLevsNA <- deepLevs
        deepLevsNA[[c(1,2,1)]][1] <- NA  # Kill  5A,B,C  
        deepLevsNA[[c(1,1,1,2)]] <- deepLevsNA[[c(1,1,1,2)]][-3]   # Kill 2C

        ## EXAMPLE
        str(deepLevs)
        deepLevs[[c(1,2,2,2,3)]]


        ## GOAL:  ***************
        getElemsR(deepLevs, 3, 5)
        getElemsR(deepLevsNA, 3, 5)
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!#
deepLevs



#####################################
#  ORIGINALLY BEGAN DOING BY HAND...
lev4 <- paste0("D", 1:8)
lev3 <- lapply(1:4, function(i) list(lev4[[2*i-1]], lev4[[2*i]]) )
lev2 <- lapply(1:2, function(i) list(lev3[[2*i-1]], lev3[[2*i]]) )
lev1 <- lapply(1:1, function(i) list(lev2[[2*i-1]], lev2[[2*i]]) )
....
#####################################
