I have 18 lists, one for each condition. Inside a list of a condiction there are 10 lists, one for each participant. Within a list of a participant, there is a list with anywhere between 1 and 20 values of type double. To clarify, this is code to reproduce the list of one condition, remember I have 18 of these all slightly different.
Participant_List <- list()
for (i in 1:10) {
Scores <- list()
for (k in sample(1:5, replace = TRUE)) {
Scores[[k]] <- sample(1:7, sample(1:10), replace = TRUE)
}
Participant_List[[i]] <- Scores
}
Now with some help, I got code to transform the list of one condition into a data frame in a long format:
#convert each participant's list to a data frame
x_dataframes <- lapply(seq_along(Participant_List), function(curParticipant){
return(data.frame(Participant = curParticipant,
Score = unlist(Participant_List[[curParticipant]])))
})
#combine the list of dataframes into one dataframe
x_combined <- do.call("rbind", x_dataframes)
I would like to create a function containing this code to be able to simply apply this to the other conditions. I came up with the following, where I first create a list containing the conditions I have, called Hypo1_lists and then I feed this into the function below:
function(Hypo1_lists){
#convert each participant's list to a data frame
x_dataframe <- lapply(seq_along(Hypo1_lists), function(curParticipant){
return(data.frame(Participant = curParticipant,
Score = unlist(Hypo1_lists[[curParticipant]])))
#combine the list of dataframes into one dataframe
Hypo1_lists <- do.call("rbind", x_dataframe)
})
}
But this outputs one nested list...I want to store the outputs in separate data frames (one for each condition), the same I get from the code before I put it into a function.