复制数据框行并添加 ID; ID 存储在列数可变的列表中

Copy dataframe rows and add ID; ID stored in list with variable number of columns

我有一个整数向量列表,我想用它来扩展我的数据框。此列表中的每个向量依次对应于我的数据帧的每一行。

对于列表中的每个向量,我想复制数据框中的相应行,创建与向量中的项目一样多的行。然后我想在这些重复的行中添加一列,其中包含向量保存的年份。 (下面的示例代码/数据)

我如何使用 dplyr 或大多数 simply/clearly 来做到这一点?

evlist <- list("75931" = c(2018, 2018, 2017), "75932" =  c(2003

# $`75931`
# [1] 2018 2018 2017
# 
# $`75932`
# [1] 2003

id_df <- data.frame(gid = c(183253, 183254, 183255, 183256))
  evlist <- list("75931" = c(2018, 2018, 2017),
                 "75932" =  c(2003, NA, NA))
# gid
# 1 183253
# 2 183254
# 3 183255
# 4 183256

# Goal DataFrame:
goal <- data.frame(gid = c(183253, 183253, 183253, 183254, 183255, 183256), 
                   year = c(2018, 2018, 2017, 2003, NA, NA))

#     gid year
# 1 183253 2018
# 2 183253 2018
# 3 183253 2017
# 4 183254 2003
# 5 183255   NA
# 6 183256   NA

您可以使用 动词来解决这个问题,将您的 evlistleft_join 重塑为带有行匹配索引的数据框。

library(tibble)
library(tidyr)
library(dplyr)

evlist <- list(
  "75931" = c(2018, 2018, 2017),
  "75932" =  c(2003)
)

id_df <- tibble(
  gid = c(183253, 183254, 183255, 183256)
)

id_df %>%
  rowid_to_column %>%                  # add index for matching
  left_join(
    evlist %>%
      t %>%                            # using transpose to pivot rows~columns
      as_tibble %>%                    # recast into a data frame for subsequent steps
      gather(rowid, year) %>%          # use gather to nest the year values
      mutate(rowid = row_number()) %>% # transform the "names" of evlist to the index
      unnest,                          # flatten the new evlist
    by = "rowid"
  ) %>%
  select(-rowid)                       # drop the index now that it served its purpose

# # A tibble: 6 x 2
#      gid  year
#    <dbl> <dbl>
# 1 183253  2018
# 2 183253  2018
# 3 183253  2017
# 4 183254  2003
# 5 183255    NA
# 6 183256    NA