0

Original List

Following is a list I am working with. As more data are gathered I'll get more similar lists each with varying number of elements (also lists):

> str(ADOs)
List of 2
 $ TrucPropk:List of 8
  ..$ distance         : num [1:13626, 1] 0 0.784 1.569 2.354 3.137 ...
  ..$ frames           : int [1:13626(1d)] 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 ...
  ..$ heading          : num [1:13626, 1] 3.14 3.14 3.14 3.14 3.14 ...
  ..$ pitch            : num [1:13626, 1] 0.00 0.00 1.70e-05 1.70e-05 5.88e-05 ...
  ..$ pos              : num [1:13626, 1:3] -9524 -9524 -9524 -9524 -9524 ...
  ..$ relative_distance: num [1:13626, 1] 92.6 92.6 92.6 92.6 92.6 ...
  ..$ roll             : num [1:13626, 1] 0.00 0.00 1.00e-36 1.00e-36 -4.48e-05 ...
  ..$ speed            : num [1:13626, 1] 14.3 14.3 14.4 14.4 14.3 ...
 $ Truck    :List of 8
  ..$ distance         : num [1:13092, 1] 0 1.34 2.69 4.03 5.38 ...
  ..$ frames           : int [1:13092(1d)] 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 ...
  ..$ heading          : num [1:13092, 1] 3.14 3.14 -3.14 -3.14 -3.14 ...
  ..$ pitch            : num [1:13092, 1] 0.00 0.00 1.01e-06 1.01e-06 3.20e-06 ...
  ..$ pos              : num [1:13092, 1:3] -9512 -9512 -9512 -9512 -9512 ...
  ..$ relative_distance: num [1:13092, 1] -268 -268 -267 -267 -267 ...
  ..$ roll             : num [1:13092, 1] 0.00 0.00 1.78e-05 1.78e-05 8.86e-05 ...
  ..$ speed            : num [1:13092, 1] 24.6 24.6 24.6 24.6 24.6 ...

In the list above, there are 2 lists which have 8 elements. But these numbers can change in other lists.

Goal

I want to split the original list into separate data frames (2 in this case) having same names ('TrucPropk' and 'Truck' in this example). I want to do this within the code for a shiny app.

What I have tried

Googling results in this answer as the top result. This splits the list into lists and stores into global environment. But I want to split into data frames and use them within the app.

Question

How can I convert the lists inside a list into separate data frames for use inside shiny app? Is there a dplyr solution?

Community
  • 1
  • 1
umair durrani
  • 5,597
  • 8
  • 45
  • 85

1 Answers1

1

Think of data.frames as lists with a few more attributes, so it's easy to convert between the two.

res <- lapply(ADOs, data.frame)

Test case:

tst <- list(as.list(mtcars), as.list(iris))
res <- lapply(tst, data.frame)
Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Thanks. This converts the individual lists to data frames but I still can't call them by names i.e. 'mtcars' and 'iris' – umair durrani Aug 31 '15 at 22:09
  • @umairdurrani does your list have a names attribute? If so, try `res <- setNames(lapply(ADOs, data.frame), names(ADOs))` – Rorschach Aug 31 '15 at 22:12