DPatrick
DPatrick

Reputation: 431

How to convert a tibble with list column to a list of dataframes

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]> |
+--------+------------------+
$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 
+--------+-----------------------+
| 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]> |
+--------+-----------------------+
+-----+-------+-------+
|     | 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

Answers (1)

Cettt
Cettt

Reputation: 11981

You can use map to extract the components you want:

param_table %>% 
 mutate(param = map(param, ~.x$result))

Upvotes: 1

Related Questions