I am working with different models that all have different parameters. It is convenient for me to store them in a database. When I pull them, they come in the form of a dataframe that I call df
.
In df
there are several columns that help differentiate each parameter from one another so that each (entire) row is ultimately unique.
For example
col_1 <- c("model_1", "model_1", "model_1", "model_1", "model_2", "model_2", "model_2", "model_2")
col_2 <- c("category_1", "category_1", "category_2", "category_2", "category_1", "category_1", "category_2", "category_2")
col_3 <- c("type_1", "type_2", "type_1", "type_2", "type_1", "type_2", "type_1", "type_2")
col_4 <- c("name_1", "name_2", "name_3", "name_4", "name_5", "name_6", "name_7", "name_8")
col_5 <- c("value_1", "value_2", "value_3", "value_4", "value_5", "value_6", "value_7", "value_8")
mat <- matrix(c(col_1, col_2, col_3, col_4, col_5), ncol = 5)
df <- data.frame(mat)
names(df) <- c("model", "category", "type", "name", "value")
I would be interested in transforming
df
into a list of list of list ... - call itdeep_list
- so that each parameter value could be accessed likeparameter <- deep_list$model_1$category_2$type_2$name_4
and it should give me
value_4
.
I've been reading this thread Converting a data.frame to a list of lists and tried to make the best use of the dlply()
function from {plyr}
as
not_deep_list <- dlply(df,1,c)
or also
not_list <- df %>% group_by(model)
I reckon this is a very similar problem (hence the similar title).
However it is different in the sense that it requires to treat more "layers" (i.e. the columns) of information hence the deep_list
name and the title...
Any suggestion is welcomed (recursions, loops, vectorized solutions, functions-from-packages-I-never-heard-of, ...)
Thanks !