【发布时间】:2021-04-05 04:02:13
【问题描述】:
我见过很多类似的问题,但无法适应我的情况。我有以嵌套列表形式出现的数据,并希望以某种方式将其转换为数据框。
my_data_object <-
list(my_variables = list(
age = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "what is your age?",
valueDescriptions = NULL
),
med_field = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 6L,
title = "what medical branch are you at?",
valueDescriptions = list(card = "Cardiology", ophth = "Ophthalmology",
derm = "Dermatology")
),
covid_vaccine = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 8L,
title = "when do you plan to get vaccinated?",
valueDescriptions = list(
next_mo = "No later than next month",
within_six_mo = "No later than six months from now",
never = "I will not get vaccinated"
)
)
))
所需的输出
var_name type originType title
<chr> <chr> <chr> <chr>
1 age numeric slider what is your age?
2 med_field string choice what medical branch are you at?
3 covid_vaccine string choice when do you plan to get vaccinated?
我的失败尝试
library(tibble)
library(tidyr)
my_data_object %>%
enframe() %>%
unnest_longer(value) %>%
unnest(value)
## # A tibble: 18 x 3
## name value value_id
## <chr> <named list> <chr>
## 1 my_variables <chr [1]> age
## 2 my_variables <chr [1]> age
## 3 my_variables <named list [0]> age
## 4 my_variables <int [1]> age
## 5 my_variables <chr [1]> age
## 6 my_variables <NULL> age
## 7 my_variables <chr [1]> med_field
## 8 my_variables <chr [1]> med_field
## 9 my_variables <named list [0]> med_field
## 10 my_variables <int [1]> med_field
## 11 my_variables <chr [1]> med_field
## 12 my_variables <named list [3]> med_field
## 13 my_variables <chr [1]> covid_vaccine
## 14 my_variables <chr [1]> covid_vaccine
## 15 my_variables <named list [0]> covid_vaccine
## 16 my_variables <int [1]> covid_vaccine
## 17 my_variables <chr [1]> covid_vaccine
## 18 my_variables <named list [3]> covid_vaccine
我正在尝试使用tidyverse 函数来实现这一点,但到目前为止,我似乎没有朝着正确的方向前进。我将不胜感激。
编辑
与我最初提供的示例数据不同,实际上我的数据具有不同的层次结构。我认为一旦我有了方法,这将很容易概括,但事实并非如此。因此,如果我们认为数据如下所示,但实际上我只关心my_variables 子列表。
my_data_object_2 <-
list(
other_variables = list(
whatever_var_1 = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "blah question",
valueDescriptions = NULL
)
),
my_variables = list(
age = list(
type = "numeric",
originType = "slider",
originSettings = structure(list(), .Names = character(0)),
originIndex = 5L,
title = "what is your age?",
valueDescriptions = NULL
),
med_field = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 6L,
title = "what medical branch are you at?",
valueDescriptions = list(card = "Cardiology", ophth = "Ophthalmology",
derm = "Dermatology")
),
covid_vaccine = list(
type = "string",
originType = "choice",
originSettings = structure(list(), .Names = character(0)),
originIndex = 8L,
title = "when do you plan to get vaccinated?",
valueDescriptions = list(
next_mo = "No later than next month",
within_six_mo = "No later than six months from now",
never = "I will not get vaccinated"
)
)
)
)
那么我怎样才能“放大”/“提取”my_variables 并且只有这样才能获得我在上面的“所需输出”中指定的表格?
【问题讨论】:
标签: r tidyr nested-lists purrr tibble