0

I have 963 lists, all containing the same type of information per list instance. The amount of data in any list at a given instance can vary, however. Instead of creating many lists, is there an efficient way to group the lists? Examples follow.

list001 <- c(originApt = 'ATL', destinApt = 'BOS', flightIndxs = c( 1 : 7 ) )
list002 <- c(originApt = 'ATL', destinApt = 'DEN', flightIndxs = c( 9 :19 ) )
:
list963 <- c(originApt = 'DCA', destinApt = 'TPA', flightIndxs = c( 8582, 8583, 8584, 8585, 8586, 8587 ) )

and so forth. Note that the length of integers in the third entry of each list varies in length. In matlab, I'd just construct a structure called 'flight' with an index for each list instance. Is there a way to organize my lists in R short of having many individual instances?

Benjamin Levy
  • 333
  • 6
  • 19
  • Instead of `c`, use `list`: like `list(originApt = 'ATL', destinApt = 'BOS', flightIndxs = 1:7)`. If you use `c`, then all elements will be coerced to character. [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) may also be worth reading. It refers to a list of data.frames, but data.frames are actually lists themselves. My answer there should help automate list creation. – lmo Feb 14 '17 at 12:38

1 Answers1

3

You can create a list of lists:

list001 <- list(originApt = 'ATL', destinApt = 'BOS', flightIndxs = c( 1 : 7 ) )
list002 <- list(originApt = 'ATL', destinApt = 'DEN', flightIndxs = c( 9 :19 ) )
large_list = list(list001, list002)
> large_list
[[1]]
[[1]]$originApt
[1] "ATL"

[[1]]$destinApt
[1] "BOS"

[[1]]$flightIndxs
[1] 1 2 3 4 5 6 7


[[2]]
[[2]]$originApt
[1] "ATL"

[[2]]$destinApt
[1] "DEN"

[[2]]$flightIndxs
 [1]  9 10 11 12 13 14 15 16 17 18 19

A list can contain any other R object as a member. Do note to not construct the sublists as c(), but also use list.

You can also create a long formatted data.frame:

do.call('rbind', lapply(large_list, function(x) as.data.frame(do.call('cbind', x))))
   originApt destinApt flightIndxs
1        ATL       BOS           1
2        ATL       BOS           2
3        ATL       BOS           3
4        ATL       BOS           4
5        ATL       BOS           5
6        ATL       BOS           6
7        ATL       BOS           7
8        ATL       DEN           9
9        ATL       DEN          10
10       ATL       DEN          11
11       ATL       DEN          12
12       ATL       DEN          13
13       ATL       DEN          14
14       ATL       DEN          15
15       ATL       DEN          16
16       ATL       DEN          17
17       ATL       DEN          18
18       ATL       DEN          19

Do note that this only works because flightIndxs is the only entry to have multiple values, and there is a clear interpretation that each flight index only has one origin and destination. It can also work with multiple variables having multiple values, as long as they all contain the same number of multiple values.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Actually, given the problem I'm working, I'd rather retain your first solution, where the integer indices are pointers to information in a data frame that contains each flight origin date/time and destination date/time. Further, I need to construct lat/lon points between the ith origin and jth destination s.t. the vector of points between any two paired locations differs in length as a function of total great-circle distance between the points. Thank you. – Benjamin Levy Feb 14 '17 at 13:12
  • The first solution is more close to the structure solution from matlab, so I can imagine you want to go for that. If you want to create custom data structures like this, have a look at S3, S4 or reference objects. [This SO question](http://stackoverflow.com/questions/9521651/r-and-object-oriented-programming) is posted some time ago contains a lot of information. – Paul Hiemstra Feb 14 '17 at 14:09
  • In addition, you can flag my answer as the correct one by clicking on the tick mark at the top of my answer, just below the up/downvote buttons. – Paul Hiemstra Feb 14 '17 at 14:11
  • So, you seem to recommending that I look at ReferenceClasses? – Benjamin Levy Feb 14 '17 at 14:31
  • Either form of OO is fine, I used S3 the most in my last projects. It was just that you wanted to create structures, which fits best with some kind of OO in R. – Paul Hiemstra Feb 15 '17 at 08:35