I have a list of lists, each of them with format like the following:
list( name = 'foo',
type = 'bar',
attributes= list(
list(name='color', value='blue' ),
list(name='batch', value=123),
list(name='is_available', value='true') )
)
i.e. with pairs of names and values of attributes. How I can transform this into this format
list( name = 'foo',
type = 'bar',
attributes= list(
color='blue' ,
batch=123,
is_available=TRUE )
)
Preferably in a tidyverse
way.
Here is a one-liner base R way. The only difference from the expected output is in the attribute is_available
, see at end.
lst <- list( name = 'foo',
type = 'bar',
attributes= list(
list(name='color', value='blue' ),
list(name='batch', value=123),
list(name='is_available', value = 'true') )
)
out <- list( name = 'foo',
type = 'bar',
attributes= list(
color='blue' ,
batch=123,
is_available = TRUE )
)
lst$attributes <- sapply(lst$attributes, \(x) setNames(x[2], x[1]))
lst$attributes$is_available
#> [1] "true"
out$attributes$is_available
#> [1] TRUE
Created on 2023-05-27 with reprex v2.0.2