将数据框列添加到列表列

时间:2021-06-28 17:57:53

标签: r list dataframe

在下面的示例中,我有国家和它们以两种不同格式(数据框和列表)指向不同模块的链接。我想将数据框 redf 中的数据(链接和给定模块)添加到与国家/地区名称匹配的列表 Nodesrelist 列的元素中。有没有办法做到这一点?我的预期输出是保留 Nodesrelist 列的表格列表格式,但还包括 redf 中的链接和模块。例如阿尔及利亚,模块 3 中来自 redf 的链接 2 将添加到阿尔及利亚的Nodes 元素中,因此其 Nodesc(`1` = 3, `3` = 5, `4` = 3) 更改为 c(`1` = 3, `2` = 3, `3` = 5, `4` = 3).

可重现的例子:

redf <- structure(list(name = c("Afghanistan", "Albania", "Algeria", 
"Angola", "Antigua and Barbuda", "Argentina"), module = c(3L, 
4L, 2L, 2L, 1L, 2L), degs = c(Afghanistan = 3, Albania = 9, Algeria = 3, 
Angola = 2, `Antigua and Barbuda` = 1, Argentina = 37)), row.names = c(NA, 
-6L), class = c("tbl_df", "tbl", "data.frame"))

relist <- structure(list(Label = structure(1:6, .Label = c("Afghanistan", 
"Albania", "Algeria", "Angola", "Antigua and Barbuda", "Argentina", 
"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahrain", 
"Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", 
"Bhutan", "Bolivia (Plurinational State of)", "Bosnia and Herzegovina", 
"Botswana", "Brazil", "Brunei Darussalam", "Bulgaria", "Burkina Faso", 
"Burundi", "C?te d'Ivoire", "Cambodia", "Cameroon", "Canada", 
"Central African Republic", "Chile", "China", "China, Hong Kong SAR", 
"China, Macao SAR", "China, Taiwan Province of", "Colombia", 
"Congo", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czechia", 
"Democratic People's Republic of Korea", "Democratic Republic of the Congo", 
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", 
"Eritrea", "Estonia", "Eswatini", "Ethiopia", "Finland", "France", 
"Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada", 
"Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", 
"Hungary", "India", "Indonesia", "Iran (Islamic Republic of)", 
"Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", 
"Kazakhstan", "Kenya", "Kuwait", "Kyrgyzstan", "Lao People's Democratic Republic", 
"Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Lithuania", 
"Luxembourg", "Madagascar", "Malawi", "Malaysia", "Mali", "Malta", 
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco", 
"Mozambique", "Myanmar", "Namibia", "Nepal", "Netherlands", "New Zealand", 
"Nicaragua", "Niger", "Nigeria", "North Macedonia", "Norway", 
"Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", 
"Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Qatar", 
"Republic of Korea", "Republic of Moldova", "Romania", "Russian Federation", 
"Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", 
"Saudi Arabia", "Senegal", "Serbia", "Sierra Leone", "Singapore", 
"Slovakia", "Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka", 
"Sudan", "Suriname", "Sweden", "Switzerland", "Syrian Arab Republic", 
"Tajikistan", "Thailand", "Timor-Leste", "Trinidad and Tobago", 
"Tunisia", "Turkey", "Turkmenistan", "Uganda", "Ukraine", "United Arab Emirates", 
"United Kingdom of Great Britain and Northern Ireland", "United Republic of Tanzania", 
"United States of America", "Uruguay", "Uzbekistan", "Venezuela (Bolivarian Republic of)", 
"Viet Nam", "Yemen", "Zambia", "Zimbabwe"), class = "factor"), 
    Nodes = list(Afghanistan = structure(c(`2` = 1L, `3` = 4L, 
    `6` = 1L), .Dim = 3L, .Dimnames = structure(list(c("2", "3", 
    "6")), .Names = ""), class = "table"), Albania = structure(c(`1` = 1L, 
    `2` = 2L, `3` = 2L, `4` = 10L), .Dim = 4L, .Dimnames = structure(list(
        c("1", "2", "3", "4")), .Names = ""), class = "table"), 
        Algeria = structure(c(`1` = 3L, `2` = 4L, `3` = 5L, `4` = 3L
        ), .Dim = 4L, .Dimnames = structure(list(c("1", "2", 
        "3", "4")), .Names = ""), class = "table"), Angola = structure(c(`2` = 3L, 
        `3` = 2L, `4` = 3L, `5` = 1L), .Dim = 4L, .Dimnames = structure(list(
            c("2", "3", "4", "5")), .Names = ""), class = "table"), 
        `Antigua and Barbuda` = structure(c(`1` = 2L), .Dim = 1L, .Dimnames = structure(list(
            "1"), .Names = ""), class = "table"), Argentina = structure(c(`0` = 2L, 
        `1` = 24L, `2` = 38L, `3` = 25L, `4` = 29L, `5` = 2L), .Dim = 6L, .Dimnames = structure(list(
            c("0", "1", "2", "3", "4", "5")), .Names = ""), class = "table"))), row.names = c(NA, 
6L), class = "data.frame")

1 个答案:

答案 0 :(得分:2)

我们通过'name'列split'redf,然后使用map2循环遍历列表的intersect名称和'Nodes'list , 并连接

library(purrr)
library(tibble)
lst1 <- map(split(redf[-1], redf$name), deframe)
nm1 <- intersect(names(relist$Nodes), names(lst1))
relist$Nodes[nm1] <- map2(relist$Nodes[nm1], lst1[nm1],
        ~  {v1 <- c(.x, .y)
         v1[order(as.numeric(names(v1)))]
    })

或者也可以在连接后使用 right_join 与 'redf' by 来自各个数据集的 Labelname 列,使用 pmap 来在行上循环,并在为 'degs/module' 列创建命名向量后进行连接

library(dplyr)
relist %>% 
   right_join(redf, by = c("Label" = "name")) %>% 
   transmute(Label, Nodes = pmap(list(Nodes, degs, module),
        ~ c(..1, setNames(..2, ..3)))) 
相关问题