for loops and apply

#  IS A FOR LOOP BETTER IN THIS SITUTAION? 

# Trying to assign names to an inner list of res


  #-------------------------
  # this works
  for (i in 1:length(res))
      dimnames(res[[i]]) <- list(myDF$group, myDF$value[[i]])

  res  # now has appropriate names


  #------------------------
  # this does not work;  scope of dimnames(.) is within the function(i)
  lapply(1:length(res), function(i)
        dimnames(res[[i]]) <- list(myDF$group, myDF$value[[i]]) )
  
  res  # still no names


  #------------------------
  # this DOES work, but probably slow
  res <- lapply(1:length(res), function(i) {
              dimnames(res[[i]]) <- list(myDF$group, myDF$value[[i]]) 
              return(res[[i]])
            }
          )

  #########################################
  ##           TIMING IT                 ##
  ##                                     ##

  f.for <- function() {
      for (i in 1:length(res))
      dimnames(res[[i]]) <- list(myDF$group, myDF$value[[i]])
  }

  f.lapply <- function() {
      res <- lapply(1:length(res), function(i) {
                  dimnames(res[[i]]) <- list(myDF$group, myDF$value[[i]]) 
                  return(res[[i]])
                }
              )
  }

  library(microbenchmark)
  microbenchmark(f.for, f.lapply, times=30000)
    #   Unit: nanoseconds
    #       expr min lq median uq   max
    # 1    f.for  57 65     65 73 23412
    # 2 f.lapply  60 65     65 76 17029
    #
    #    HM.... practically negligible.  notice units is NANOSECONDS

The for loop is probably cleaner in this case. 


################################
#------------------------------#
# here is where res came from: #

  # original data
  require(plyr)
  myDF <- data.frame(groups=paste0("Grp", 1:7))
  myDF <- adply(myDF, 1, function(x) data.frame(value=t(list(sample(LETTERS, 3)))))

  # res is based on myDF
  res<- 
  lapply(myDF$value, function(L1) 
      t(sapply(myDF$value, function(L2) L1 %in% L2 ))
  )

  res_bak <- res
