Reputation: 431
I have a tibble
that looks like this (let's call it param_table
):
+--------+------------------+
| name | param |
+--------+------------------+
| apple | <named list [2]> |
+--------+------------------+
| orange | <named list [2]> |
+--------+------------------+
| pear | <named list [2]> |
+--------+------------------+
| banana | <named list [2]> |
+--------+------------------+
map(safely(...)...)
to different fruits with a customized function.param_table$param[[1]]
will return the result for apple:$result
+-----+-------+-------+
| | alpha | beta |
+-----+-------+-------+
| 1 | 0.1 | 1.1 |
+-----+-------+-------+
| 2 | 0.2 | 1.2 |
+-----+-------+-------+
| 3 | 0.3 | 1.3 |
+-----+-------+-------+
| 4 | 0.4 | 1.4 |
+-----+-------+-------+
| 5 | 0.5 | 1.5 |
+-----+-------+-------+
| ... | ... | ... |
+-----+-------+-------+
(There are in total 10,000 rows, skipping the rest here)
$error
NULL
I am writing a shiny app where I need to directly call the table in $result
as a dataframe. I can not simply do sth like as.data.frame(param_table$param[[1]]$result)
because this is manual to individual fruit & not applicable to all fruits.
Ideally I would want the ending result to look like this: a tibble where the param value for each fruit is a df, instead of a named list that contains $result
and error
. In other words, I want to keep the $result
for each fruit while throwing away $error
.
I would like the tibble to look like this
+--------+-----------------------+
| name | param |
+--------+-----------------------+
| apple | <df[,2] [10,000 × 2]> |
+--------+-----------------------+
| orange | <df[,2] [10,000 × 2]> |
+--------+-----------------------+
| pear | <df[,2] [10,000 × 2]> |
+--------+-----------------------+
| banana | <df[,2] [10,000 × 2]> |
+--------+-----------------------+
param_table$param[[1]]
, it would return me the result for apple as such:+-----+-------+-------+
| | alpha | beta |
+-----+-------+-------+
| 1 | 0.1 | 1.1 |
+-----+-------+-------+
| 2 | 0.2 | 1.2 |
+-----+-------+-------+
| 3 | 0.3 | 1.3 |
+-----+-------+-------+
| 4 | 0.4 | 1.4 |
+-----+-------+-------+
| 5 | 0.5 | 1.5 |
+-----+-------+-------+
| ... | ... | ... |
+-----+-------+-------+
Should I write a function to make such conversion? Something like:
for (i in names){
as.data.frame(param_table1[[as.numeric(which(names == i))]])
}
Much appreciation for your help!
Upvotes: 0
Views: 188
Reputation: 11981
You can use map
to extract the components you want:
param_table %>%
mutate(param = map(param, ~.x$result))
Upvotes: 1