简体   繁体   English

Dataframe 从一个向量和一个向量列表通过复制元素

[英]Dataframe from a vector and a list of vectors by replicating elements

I have a vector and list of the same length.我有一个相同长度的向量和列表。 The list contains vectors of arbitrary lengths as such:该列表包含任意长度的向量,如下所示:

vec1 <- c("a", "b", "c")

list1 <- list(c(1, 3, 2),
              c(4, 5, 8, 9),
              c(5, 2))

What is the fastest, most effective way to create a dataframe such that the elements of vec1 are replicated the number of times corresponding to their index in list1?创建 dataframe 以使 vec1 的元素复制与其在 list1 中的索引相对应的次数的最快、最有效的方法是什么?

Expected output:预期 output:

#   col1 col2
# 1    a    1
# 2    a    3
# 3    a    2
# 4    b    4
# 5    b    5
# 6    b    8
# 7    b    9
# 8    c    5
# 9    c    2

I have included a tidy solution as an answer, but I was wondering if there are other ways to approach this task.我已经包含了一个整洁的解决方案作为答案,但我想知道是否还有其他方法可以完成这项任务。

In base R , set the names of the list with 'vec1' and use stack to return a two column data.framebase R中,使用 'vec1' 设置list的名称并使用stack返回两列 data.frame

stack(setNames(list1, vec1))[2:1]

-output -输出

  ind values
1   a      1
2   a      3
3   a      2
4   b      4
5   b      5
6   b      8
7   b      9
8   c      5
9   c      2

If we want a tidyverse approach, use enframe如果我们想要一个 tidyverse 方法,请使用enframe

library(tibble)
library(dplyr)
library(tidyr)
list1 %>% 
 set_names(vec1) %>% 
 enframe(name = 'col1', value = 'col2') %>% 
 unnest(col2)
# A tibble: 9 × 2
  col1   col2
  <chr> <dbl>
1 a         1
2 a         3
3 a         2
4 b         4
5 b         5
6 b         8
7 b         9
8 c         5
9 c         2

This tidy solution replicates the vec1 elements according to the nested vector's lengths, then flattens both lists into a tibble.这个整洁的解决方案根据嵌套向量的长度复制 vec1 元素,然后将两个列表展平为一个小标题。

library(purrr)
library(tibble)

tibble(col1 = flatten_chr(map2(vec1, map_int(list1, length), function(x, y) rep(x, times = y))),
           col2 = flatten_dbl(list1))

# # A tibble: 9 × 2
#   col1   col2
#   <chr> <dbl>
# 1 a         1
# 2 a         3
# 3 a         2
# 4 b         4
# 5 b         5
# 6 b         8
# 7 b         9
# 8 c         5
# 9 c         2

A tidyr / tibble -approach could also be unnest_longer : tidyr / tibble方法也可以是unnest_longer

library(dplyr)
library(tidyr)

tibble(vec1, list1) |> 
  unnest_longer(list1)

Output: Output:

# A tibble: 9 × 2
  vec1  list1
  <chr> <dbl>
1 a         1
2 a         3
3 a         2
4 b         4
5 b         5
6 b         8
7 b         9
8 c         5
9 c         2

Another possible solution, based on purrr::map2_dfr :另一种可能的解决方案,基于purrr::map2_dfr

library(purrr)

map2_dfr(vec1, list1, ~ data.frame(col1 = .x, col2 =.y))

#>   col1 col2
#> 1    a    1
#> 2    a    3
#> 3    a    2
#> 4    b    4
#> 5    b    5
#> 6    b    8
#> 7    b    9
#> 8    c    5
#> 9    c    2

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

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