简体   繁体   English

从 tibble 创建列表列表

[英]Create list of lists from tibble

I'm trying to do something that seems very simple, but I cannot figure it out.我正在尝试做一些看起来很简单的事情,但我无法弄清楚。 I have a tibble like so:我有一个像这样的小标题:

> df <- tibble::tribble(
  ~col_a, ~col_b,
  1,      "A",   
  2,      "B",   
  3,      "C",   
)
> df
# # A tibble: 3 x 2
# col_a col_b
# <dbl> <chr>
#   1     A    
#   2     B    
#   3     C 

and I want to turn it into a list that looks like this我想把它变成一个看起来像这样的列表

> str(res_list)
# List of 3
# $ :List of 2
# ..$ col_a: num 1
# ..$ col_b: chr "A"
# $ :List of 2
# ..$ col_a: num 2
# ..$ col_b: chr "B"
# $ :List of 2
# ..$ col_a: num 3
# ..$ col_b: chr "C"

I tried a number of things using base apply and dplyr::rowwise but nothing worked quite right.我使用 base applydplyr::rowwise尝试了很多东西,但没有一个效果很好。 In the docs for purrr::pmap I thought I found the answer:purrr::pmap的文档中,我想我找到了答案:

f.   A function, formula, or vector (not necessarily atomic)...

If character vector, numeric vector, or list, it is converted to an extractor function. Character vectors index by name...

So I thought, great this should work: pmap(df, c("col_a", "col_b")) and that should extract those columns for each element (row) and return a list of the extracted lists.所以我想,这应该很好用: pmap(df, c("col_a", "col_b"))并且应该为每个元素(行)提取这些列并返回提取列表的列表。 But when I run that I get:但是当我运行时,我得到:

 Error in pluck(x, "col_a", "col_b", .default = NULL) : 
  argument "x" is missing, with no default 

I semi-understand this error, but I think I'm following the usage in the docs.我半理解这个错误,但我认为我正在关注文档中的用法。 Maybe this is just a bug in purrr?也许这只是 purrr 中的一个错误?

Anyway, commentary on the potential purrr bug is welcome, but really I'm just trying to create this list.无论如何,欢迎对潜在的 purrr 错误进行评论,但实际上我只是想创建这个列表。 Any help is very appreciated.非常感谢任何帮助。

You can use group_split() (or base split() if you prefer):您可以使用group_split() (或基本split()如果您愿意):

library(dplyr)
library(purrr)

df %>%
  group_split(row_number(), .keep = FALSE) %>%
  map(as.list)

Where str() gives:其中str()给出:

List of 3
 $ :List of 2
  ..$ col_a: num 1
  ..$ col_b: chr "A"
 $ :List of 2
  ..$ col_a: num 2
  ..$ col_b: chr "B"
 $ :List of 2
  ..$ col_a: num 3
  ..$ col_b: chr "C"

Or:或者:

lapply(split(df, 1:nrow(df)),
       as.list)

You can use asplit() :您可以使用asplit()

type.convert(lapply(asplit(df, 1), as.list), as.is = TRUE)

or by()by()

`attributes<-`(by(df, 1:nrow(df), as.list), NULL)

Both of str() give str()都给出

List of 3
 $ :List of 2
  ..$ col_a: int 1
  ..$ col_b: chr "A"
 $ :List of 2
  ..$ col_a: int 2
  ..$ col_b: chr "B"
 $ :List of 2
  ..$ col_a: int 3
  ..$ col_b: chr "C"

We can use pmap我们可以使用pmap

library(purrr)
dflist <- df %>%
              pmap(~ list(...))
str(dflist)
#List of 3
# $ :List of 2
#  ..$ col_a: num 1
#  ..$ col_b: chr "A"
# $ :List of 2
#  ..$ col_a: num 2
#  ..$ col_b: chr "B"
# $ :List of 2
#  ..$ col_a: num 3
#  ..$ col_b: chr "C"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM