Home > Back-end >  How to convert a table to a named list where each list element is a row from the table?
How to convert a table to a named list where each list element is a row from the table?

Time:04-14

Let's say I have the following dataset:

fruits <- tibble(
  fruit = c("apple", "orange", "pear", "pineapple", "blueberry"),
  color = c("red", "orange", "green", "yellow", "blue"),
  climate = c("temperate", "(sub)tropical", "temperate", "(sub)tropical", "temperate"),
  sugar_per_100g = c(10.39, 9.35, 9.75, 9.85, 9.96)
)
fruit color climate sugar_per_100g
apple red temperate 10.39
orange orange (sub)tropical 9.35
pear green temperate 9.75
pineapple yellow (sub)tropical 9.85
blueberry blue temperate 9.96

Now I want to create some sort of lookup list where each list element contains the data for a certain fruit in the data set.

The resulted list should be this:

## $apple
## $apple$color
## [1] "red"
## 
## $apple$climate
## [1] "temperate"
## 
## $apple$sugar_per_100g
## [1] 10.39
## 
## 
## $orange
## $orange$color
## [1] "orange"
## 
## $orange$climate
## [1] "(sub)tropical"
## 
## $orange$sugar_per_100g
## [1] 9.35
## 
## ...

How can I do this in R?

CodePudding user response:

You can try this

> setNames(lapply(1:nrow(fruits), function(k) as.list(fruits[k, -1])), fruits[[1]])
$apple
$apple$color
[1] "red"

$apple$climate
[1] "temperate"

$apple$sugar_per_100g
[1] 10.39


$orange
$orange$color
[1] "orange"

$orange$climate
[1] "(sub)tropical"

$orange$sugar_per_100g
[1] 9.35


$pear
$pear$color
[1] "green"

$pear$climate
[1] "temperate"

$pear$sugar_per_100g
[1] 9.75


$pineapple
$pineapple$color
[1] "yellow"

$pineapple$climate
[1] "(sub)tropical"

$pineapple$sugar_per_100g
[1] 9.85


$blueberry
$blueberry$color
[1] "blue"

$blueberry$climate
[1] "temperate"

$blueberry$sugar_per_100g
[1] 9.96

CodePudding user response:

This can be boiled down into 3 basic steps:

  1. Remove the column that will be used for naming from the data, because we don't want it to be a sub element of the list. Keep in mind, that the values of the naming column must be unique.
  2. Create (sub)lists for each each row or in the table. Or in the variant: transpose the data set (output would be a list).
  3. Apply names to the top level list elements. The names originate from the naming column.
fruits %>%
  select(-fruit) %>%         # remove column that will be used for naming
  pmap(list) %>%             # convert rows to list
  set_names(fruits$fruit)    # name list with naming column

Variant:

fruits %>%
  select(-fruit) %>%
  transpose(.names = data$fruit)

CodePudding user response:

You can do this as a one-liner in base R:

setNames(lapply(asplit(fruits[-1], 1), as.list), fruits[[1]])
#> $apple
#> $apple$color
#> [1] "red"
#> 
#> $apple$climate
#> [1] "temperate"
#> 
#> $apple$sugar_per_100g
#> [1] "10.39"
#> 
#> 
#> $orange
#> $orange$color
#> [1] "orange"
#> 
#> $orange$climate
#> [1] "(sub)tropical"
#> 
#> $orange$sugar_per_100g
#> [1] " 9.35"
#> 
#> 
#> $pear
#> $pear$color
#> [1] "green"
#> 
#> $pear$climate
#> [1] "temperate"
#> 
#> $pear$sugar_per_100g
#> [1] " 9.75"
#> 
#> 
#> $pineapple
#> $pineapple$color
#> [1] "yellow"
#> 
#> $pineapple$climate
#> [1] "(sub)tropical"
#> 
#> $pineapple$sugar_per_100g
#> [1] " 9.85"
#> 
#> 
#> $blueberry
#> $blueberry$color
#> [1] "blue"
#> 
#> $blueberry$climate
#> [1] "temperate"
#> 
#> $blueberry$sugar_per_100g
#> [1] " 9.96"

Created on 2022-04-13 by the reprex package (v2.0.1)

  • Related