Make complicated list into a dataset

Hey,

I have a problem converting a list into a dataset. It has different dropdown options within the list.

1: Different sessions (games)
image

2: When I press on one of the sessions to watch data for the specific session I can go into Data and then press on dataset for Players

3: In here theres a list with all the players available for the specific session chosen. Only took team average observation in the screenshot - but there are player rows aswel.
image

4: When I press on "1 variable" for a given player, it opens a dataset with data of the chosen player in the chosen session:
image

Now how can I make a gathered dataset with all sessions and data per player per session in one whole dataset? I have tried different options without luck.

Commenting to move the thread up

Begin by doing a mockup of the data frame you are trying to create from the source list and run

dput(name_of_yours)

and cut and paste the output into a source window. The subset your list to no more than the first two sessions and do a dput() on that and post both.

I did a mockup of the dataframe and ran dput. Cant paste the 2nd dput with the subsetted list of 2 sessions as its too long. What to look for now?

----- 1. DPUT-----

dput(mockup_data)
structure(list(Session_ID = "1011652", Player = "TeamAverage",
Total.Distance = 11416.697076, Estimated.Energy.Expenditure = 62.917481,
Average.metabolic.power = 10.737138, X.AI = 42.440188, X..Dist.MP.20W = 28.067889,
HrAvg = 163.613446, HRmax = 190.333333, Amax = 5.689264,
Dmax = -6.721868, X.HRAvg = 84.10484, Minutes = 97.663333,
Max.speed..km.h. = 28.3504, Dist...25.km.h = 147.997054,
Dist...15.km.h = 2513.825844, Nr.entries.over.25.km.h = 9.75058,
Num...90..Smax = 3.48235, D_AccHI = 587.805448, D_DecHI = 518.317742,
Nr..acceleration...3m.s2 = 39.698789, Nr..acceleration...2m.s2 = 179.490266,
Nr.deceleration...3m.s2 = 54.225163, Nr.deceleration...2m.s2 = 169.540694,
Distance.MP.20W = 3212.087949, T.90..Hrmax = 1672.144847,
T_HR..85..max = 3330.877672, Training.Load = 3351.342844,
Dist.min = 116.898499, Nr.entries.over.20.km.h = 62.781795,
Dist...20.km.h = 675.515687, Dist.min...15.km.h = 25.73971,
Dist.min...20.km.h = 8.04667, Dist.min...25.km.h = 1.51538,
T.min..0.5W = 19.604347, Acc.Dec..2m.s2.min = 11.32588, Dist..Acc...2m.s2.min = 6.018691,
Dist..Dec...2m.s2.min = 5.307189), class = "data.frame", row.names = c(NA,
-1L))

Thanks. That part is working. For the second part, yeah, that is a lot. Please use

str(nested_list)

instead

str(subset_session_data_list)
List of 2
1011652:List of 2 .. Result: chr "OK"
.. Data :List of 2 .. .. matchOPTA_ID: NULL
.. .. Players :'data.frame': 16 obs. of 4 variables: .. .. .. PlayerName: chr [1:16] "Team Average" "Player1" "Player2" "Player3" ...
.. .. .. StatsID : logi [1:16] NA NA NA NA NA NA ... .. .. .. OptaUUID : logi [1:16] NA NA NA NA NA NA ...
.. .. .. Data :List of 16 .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 11416.7 62.9 10.7 42.4 28.1 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 3277.7 18.1 10.4 43.3 27.6 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 11231.2 61.7 11.2 43.8 29.1 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 8182.7 45.2 10.8 42.8 29.2 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 10670.5 59.6 10.2 42.5 26.7 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 11531.5 64 10.9 43.2 28.6 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 9841.8 53.9 10.5 42.1 28.1 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 12383.2 66.3 11.3 41 27.9 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 10926.8 60.2 10.3 41.8 27.3 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 1483.44 8.31 10.97 41.68 26.72 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 3054.14 17.26 9.94 41.93 26.78 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 10492.39 57.63 9.84 39.22 24.87 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 8815.6 48.6 11.5 44.4 30.1 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 815.69 4.55 11.16 41.15 25.37 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 10467.4 58.3 11.4 44.5 30.8 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 1571.5 8.7 11.3 43.9 28.9 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... 1007940:List of 2
.. Result: chr "OK" .. Data :List of 2
.. .. matchOPTA_ID: NULL .. .. Players :'data.frame': 6 obs. of 4 variables:
.. .. .. PlayerName: chr [1:6] "Team Average" "Player1" "Player2" "Player3" ... .. .. .. StatsID : logi [1:6] NA NA NA NA NA NA
.. .. .. OptaUUID : logi [1:6] NA NA NA NA NA NA .. .. .. Data :List of 6
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 11590.6 64 10.9 45.3 30.8 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 8476.5 46.5 10.8 44.1 29.5 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 7822.6 43.4 11.3 47.8 32.4 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 2988.3 16.5 13 47.7 32.3 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...
.. .. .. .. :'data.frame': 36 obs. of 4 variables: .. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ...
.. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ... .. .. .. .. .. Value : num [1:36] 7066.8 38.8 10.2 42.4 28.2 ...
.. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ... .. .. .. .. :'data.frame': 36 obs. of 4 variables:
.. .. .. .. .. Key : int [1:36] 1 3 7 11 15 19 20 27 28 46 ... .. .. .. .. .. ParameterName: chr [1:36] "Total Distance" "Estimated Energy Expenditure" "Average metabolic power" "%AI" ...
.. .. .. .. .. Value : num [1:36] 11427.9 63.4 10.8 45.9 32.2 ... .. .. .. .. .. Code : logi [1:36] NA NA NA NA NA NA ...

1 Like

Sorry for the delay. As near as I can tell from eyeballing, the source object is a list of sessions, each of which contains a list of two elements. The first element is Result, a status indicator such as OK that doesn't appear needed for mockup. The second element is Data which consists of matchOPTA_ID, which is something of a headfake for reasons discussed below and Players which is a data frame object of 16 rows and 4 columns. I'm guessing that the number of columns in Players may differ from session to session. Some of the variables appear that they might be calculated from other variables. If that is the case, I always prefer to run the calculations over again within the object that I'm creating so that I have more confidence in them.

Where it gets tricky is that ultimately the data you want to unpack are key value pairs, where the key is to be used for the variable name and the value is, well, the value. These are nested in yet another data frame structure.

After much headscratching, I found that a pairlist is an obscure form of list that is mainly used in R internals but can be otherwise treated as a list. I tried to eyeball creating a toy example of your data but didn't succeed. So here's a recipe without all the measures and ingredients.

Let's call your subset_session_data_list simply l.

names(l[1][[1]]) # may need to futz with this 

gets the session number

and

l[2][2][1]

should get the PlayerName vector and

l[2][2][4][1]

should get just a single data frame with the ultimate data, but I can't see clearly. Try it out?

I think the following code would make a simpler list containing only the Players content


# Function to recursively extract all  values stored under *keyval*  from a nested list
extract_values <- function(lst,keyval) {
  values <- list()
  
  for (key in names(lst)) {
    if (is.list(lst[[key]])) {
      values <- c(values, extract_values(lst[[key]],keyval))
    } else if (key == keyval) {
      values <- c(values, lst[[key]])
    }
  }
  
  return(values)
}

# Extract all 'Players' values using the recursive function
extract_values(session_data_list,keyval = "Players") 

depending on the dimensions of the Players object it may be meaningful to stack them with bind_rows, or else some other treatment may be needed.

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.