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.
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:
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.
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.