#  learning data.table.R
library(data.table)

#-------------------------------------------------------#
#         CREATE SAMPLE DATA                            #
#    LETTERS represent artist names                     #
#-------------------------------------------------------#
  
  # create list of delim'd strings of varying length 
  rows <- 7
  set.seed(101)
  varyingLengths <- c(sample(1:3, rows, TRUE))
  A <-  lapply(varyingLengths, function(n) sample(LETTERS[1:8], n))
  
  # create table of "recent events" with counts
  counts <- round(abs(rnorm(size)*12))   # random values
  rec_data_table <- data.table(bid=300+(1:size), counts=counts, names_list=A, key="bid")

  # create table of artist id's
  A_ids <- data.frame(name=LETTERS, id=1:26)
  A_ids <- A_ids[-c(5, 9, 12:26),]  # remove a few rows to represent artists with no IDs
  A_ids.DT <- data.table(A_ids, key="name")

#-------------------------------------------------------#


#-------------------------------------------------------#
#                                                       #
#  GOAL:  to pull metric data for each artist that we   #
#         have concert data for.                        #
#                                                       #
#         Pull data based on dates of event.            # 
#                                                       #
#-------------------------------------------------------#

#-------------------------------------------------------#
#  FIRST: create a new column in rec_data_table         #
#         that contains a list of the artistIDs of all  #
#         artists that played that event                #
#-------------------------------------------------------#

  # as a concatenated string
  rec_data_table[, artistIDs := paste(A_ids.DT[names_list, id]$id, collapse="|"), by=bid]

  # as a list
  rec_data_table[, artistIDs.list := sapply(names_list, function(n) c(A_ids.DT[n, id]$id))]

  # let's instead make the data long. 
  .
  .
  .
  .
  .
  .
  .
  .
  .

#-------------------------------------------------------#
#  SECOND: for each artistID, return a vec of event ids #
#-------------------------------------------------------#

 #-------------------------------------------------------------------#
    rec_data_table[,lapply(example_artists, function(a)
                      sapply(artistIDs.list, function(lst) a %in% lst)), by=bid]
 #-------------------------------------------------------------------#


  example_artists <- c(3, 4, 8)
  # uniqueArists_fromRecent <- example_artists

  artistEvents <- data.table(aid=uniqueArists_fromRecent, events=list(), key="aid")
  
  artistEvents[, ]
  rec_data_table[,]

  # artistEvents[, aid %in% rec_data_table$artistIDs.list[[4]], by=aid]
  artistEvents[, rec_data_table[aid %in% artistIDs.list, bid, by=bid], by=aid]

  aid <- 8
  artistEvents[, 
    bIDs := 
    list(rec_data_table[sapply(artistIDs.list, function(x) aid %in% x), bid])
  , by=aid];   artistEvents



  aid <- 8
  rec_data_table[

  artistEvents[,sapply(artistIDs.list, function(x) aid %in% x), by=aid]

  , ]
    events := 
    rec_data_table[, bid]
  , by=aid];   artistEvents


  # rec_data_table[,artistIDs.list]

 #-------------------------------------------------------------------#
    rec_data_table[,lapply(example_artists, function(a)
                      sapply(artistIDs.list, function(lst) a %in% lst)), by=bid]
 #-------------------------------------------------------------------#
 
 
  [,3 %in% artistIDs.list, by=bid]











  #----------------------#
  #  THESE ARE DIFFERENT #
  #----------------------#
    lapply(nlist, function(n) A_ids.DT[n, "id", with=FALSE])

      # - from these two, which are the same - #
    lapply(nlist, function(n) A_ids.DT[n, id])
    lapply(nlist, function(n) A_ids.DT[n, list(id)])
  #----------------------#

