I am doing dynamic network analysis in R and I have a list of date ranges as two columns in a dataframe. I am trying to figure out how to create a comprehensive dataframe/list that contains every possible unique combination of every date within each range. The date ranges are all of different lengths.
For example:
Date1 Date2
1275 1277
1301 1303
1290 1291
I want to create a dataframe or list in which each column/item represents a unique possible combination of the dates within each of these ranges:
1 2 3 4 5 6 7...........?
1275 1276 1277 1275 1275 1275 1276........1277
1301 1301 1301 1302 1303 1302 1202........1303
1290 1290 1290 1290 1290 1291 1290........1291
My intuition from java is to turn to nested loops, but avoiding loops is like the whole point of R so I feel like I must be wrong in that intuition.
Based on R- all pairwise combinations of elements from two lists and Calculating all possible combinations within a range in R I've hacked together this
makeAllDateCombos <- function(earlydatecolumn, latedatecolumn){
allDCs <- NULL
allDCs <- as.data.frame(lapply(earlydatecolumn, function(earlydatecolumn){
lapply(latedatecolumn, function(latedatecolumn){
(table(earlydatecolumn:latedatecolumn))
})
}))
return(allDCs)
}
allTheDCs <- makeAllDCs(collab_dates$Date1, collab_dates$Date2)
However this returns an error.
Error in data.frame(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, :
arguments imply differing number of rows: 16, 27, 6, 21, 26, 1, 5, 4, 31, 11, 18, 13, 15, 23, 17, 7, 10, 9, 24, 19, 33, 20