Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将包含不规则项的一级列表转换为数据框_R_List_Purrr - Fatal编程技术网

R 将包含不规则项的一级列表转换为数据框

R 将包含不规则项的一级列表转换为数据框,r,list,purrr,R,List,Purrr,我正在寻找上述问题的简单解决方案。当API返回JSON并随后转换为列表时,我似乎经常遇到这个问题 Reprex数据: result_head <- list(list(name = "JEFFREY", gender = "male", probability = 1L, count = 932L), list(name = "Jan", gender = "male", probability = 0.6, count = 1663L), list(name = "Elquis",

我正在寻找上述问题的简单解决方案。当API返回JSON并随后转换为列表时,我似乎经常遇到这个问题

Reprex数据:

result_head <- list(list(name = "JEFFREY", gender = "male", probability = 1L, 
count = 932L), list(name = "Jan", gender = "male", probability = 0.6, 
count = 1663L), list(name = "Elquis", gender = NULL), list(
name = "ELQUIS", gender = NULL), list(name = "Francisco", 
gender = "male", probability = 1L, count = 1513L))
我在这里问了一个类似的问题:

。。。但对于一个不太复杂的列表来说,解决方案完全是过度设计的

我希望有一个尽可能优雅的解决方案——我经常遇到这种操作,但似乎没有一种一致的方法来实现这一点,只需要几个层次的函数抽象

我意识到围绕这一点的问题可能已经被问到了,我可能错过了一些东西,但似乎没有一个一致和简单的方法来解决似乎是一个常见的问题

谢谢。

library(purrr)#转置和映射
library(purrr) # transpose and map_if
library(rlist) # list.stack

result_head <- list(
  list(name = "JEFFREY", gender = "male", probability = 1L, count = 932L), 
  list(name = "Jan", gender = "male", probability = 0.6, count = 1663L), 
  list(name = "Elquis", gender = NULL), 
  list(name = "ELQUIS", gender = NULL), 
  list(name = "Francisco", gender = "male", probability = 1L, count = 1513L)
)

list.stack(transpose(
  lapply(transpose(result_head), function(y) map_if(y, is.null, function(x) NA))
))
库(rlist)#list.stack
结果\u head这里是另一个选项,在
展平后使用
映射
,并转换为
tible

library(tidyverse)
map_df(result_head, ~ flatten(.x) %>%
                as_tibble)
# A tibble: 5 x 4
#  name      gender probability count
#  <chr>     <chr>        <dbl> <int>
#1 JEFFREY   male           1     932
#2 Jan       male           0.6  1663
#3 Elquis    <NA>          NA      NA
#4 ELQUIS    <NA>          NA      NA
#5 Francisco male           1    1513

或者只是
库(purrr);映射dfr(结果头,展平)
谢谢。出于兴趣,为什么map的语法调用.x而不是x?同样,在文档中,它提到.x和.f.@nycrefugee,这可能是为了防止与名为“x”的其他标识符发生冲突。不太可能有名为
.x
       name gender probability count
1   JEFFREY   male         1.0   932
2       Jan   male         0.6  1663
3    Elquis   <NA>          NA    NA
4    ELQUIS   <NA>          NA    NA
5 Francisco   male         1.0  1513
library(tidyverse)
map_df(result_head, ~ flatten(.x) %>%
                as_tibble)
# A tibble: 5 x 4
#  name      gender probability count
#  <chr>     <chr>        <dbl> <int>
#1 JEFFREY   male           1     932
#2 Jan       male           0.6  1663
#3 Elquis    <NA>          NA      NA
#4 ELQUIS    <NA>          NA      NA
#5 Francisco male           1    1513
map_dfr(result_head, flatten)