r - 通过在 for 循环中使用索引迭代地将值添加到列表中
问题描述
这是此问题的后续问题:Add two integers to each element of a list in a for loop
我想以非常指定的方式迭代地将值添加到列表中:我有这个列表:
> dput(head(word.vectors.table, 2))
structure(list(`Gina Haley` = structure(c(1L, 3L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 7L, 1L, 1L,
1L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 3L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 2L,
1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 4L,
1L), .Dim = 134L, .Dimnames = structure(list(c("after", "album",
"american", "angeles", "angelesshe", "appeared", "april", "around",
"arranging", "become", "birthdayin", "bridge", "called", "carry",
"catch", "ceremony", "charts", "chelo", "child", "christi", "clock",
"closely", "colaiuta", "comets", "concert", "continues", "corpus",
"costa", "country", "dallas", "daniel", "decided", "dedicate",
"during", "event", "falcao", "famous", "father", "fathers", "field",
"finally", "first", "followshe", "formed", "gallardo", "georgina",
"ginas", "grogan", "group", "guitarist", "haley", "haleys", "harlingen",
"heath", "hollywood", "horton", "houston", "identity", "important",
"included", "inductees", "induction", "inspired", "intended",
"japan", "jobim", "joint", "landed", "later", "legacy", "linda",
"making", "managed", "march", "marriage", "martha", "members",
"mexico", "michael", "moved", "movies", "music", "musical", "musician",
"original", "other", "paulinho", "perform", "performed", "piano",
"pioneer", "position", "preparing", "producer", "published",
"reaching", "recording", "released", "reverend", "richard", "rockabilly",
"rudolph", "sales", "selftaught", "selftitled", "sembello", "silva",
"songs", "songwriter", "sound", "south", "stalling", "start",
"state", "television", "texas", "texason", "these", "through",
"tracks", "trained", "traveled", "txshe", "vincent", "viper",
"waveduring", "which", "whose", "worked", "world", "would", "writing",
"years", "youngest")), .Names = ""), class = "table"), `Kevin Yates (politician)` = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 1L, 1L, 4L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 5L, 1L, 1L), .Dim = 63L, .Dimnames = structure(list(
c("along", "assembly", "bargaining", "before", "beyond",
"byelection", "cabinet", "canadian", "carmichael", "chief",
"community", "council", "declined", "deputy", "dewdney",
"disability", "elected", "employed", "executive", "explaining",
"february", "first", "government", "house", "issues", "kevin",
"leader", "leaderon", "lorne", "media", "minister", "moved",
"named", "negotiator", "opposition", "party", "personal",
"politician", "position", "premier", "provincial", "public",
"reasons", "reelected", "regina", "resources", "result",
"returned", "safety", "september", "served", "service", "shuffle",
"social", "sources", "speculated", "stepped", "stepping",
"which", "worker", "yates", "years", "yearsyates")), .Names = ""), class = "table")), .Names = c("Gina Haley",
"Kevin Yates (politician)"))
我还有成对的索引,其中第一对(x)表示列表中组件的编号,第二对表示 y,即 x 组件内的值。
> dput(head(index, 20))
list(c(1L, 1L), 1:2, c(1L, 3L), c(1L, 4L), c(1L, 6L), c(1L, 7L
), c(1L, 8L), c(1L, 10L), c(1L, 12L), c(1L, 13L), c(1L, 14L),
c(1L, 16L), c(1L, 17L), c(1L, 19L), c(1L, 22L), c(1L, 25L
), c(1L, 26L), c(1L, 28L), c(1L, 29L), c(1L, 30L))
我想做的是创建一个空列表(new.table
)并使用 for 循环将值插入new.table
这是重要的部分:
我想使用索引列表 ( index
) 从中提取值word.vectors.table
并将这些值放入new.table
其中,以便同一组件中的所有“提取”值word.vectors.table
都可以放入new.table
.
这是我尝试过的:
new.table <- list()
for (ii in seq(index)) {
new.table[[index[[ii]][1]]] <- c(new.table[[index[[ii]][1]]], word.vectors.table[[index[[ii]][1]]][index[[ii]][2]])
}
编辑:根据梅丽莎的评论,我添加了所需的输出(test
):
> dput(test)
list(structure(c(1L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Dim = 20L, .Dimnames = structure(list(
c("after", "album", "american", "angeles", "angelesshe",
"appeared", "april", "around", "arranging", "become", "birthdayin",
"bridge", "called", "carry", "catch", "ceremony", "charts",
"chelo", "child", "christi")), .Names = ""), class = "table"),
structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
1L, 1L, 1L), .Dim = 30L, .Dimnames = structure(list(c("along",
"assembly", "bargaining", "before", "beyond", "byelection",
"cabinet", "canadian", "carmichael", "chief", "community",
"council", "declined", "deputy", "dewdney", "disability",
"elected", "employed", "executive", "explaining", "february",
"first", "government", "house", "issues", "kevin", "leader",
"leaderon", "lorne", "media")), .Names = ""), class = "table"),
structure(c(1L, 2L, 1L, 1L, 3L, 1L, 14L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 3L, 3L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 3L), .Dim = 40L, .Dimnames = structure(list(
c("ability", "about", "affect", "against", "agents",
"agerelated", "aging", "alistair", "alzheimers", "animals",
"appeared", "applied", "applies", "associated", "attended",
"audiences", "award", "balance", "basel", "became", "behavioral",
"between", "biology", "braidhurst", "breakdown", "brown",
"called", "cancerthe", "capable", "career", "cells",
"cellular", "center", "certain", "checkpoint", "chemical",
"chemicals", "closely", "colorado", "compounds")), .Names = ""), class = "table"))
解决方案
这是使用tidyverse
套件套件的解决方案。我将从更改索引向量的格式开始:
index <- do.call(rbind, index) %>%
as_data_frame()
下一步是按第一列(现在称为V1
)拆分索引 - 这将确定word.vectors.table
我们查看的组件。最后,该map2
函数让我们获取两个列表的元素并将它们插入一个函数(因此 list1 的第一个元素和 list2 的第一个元素;list1 的第二个元素和 list2 的第二个元素,等等。
result <- result <- index %>%
split(., .$V1) %>%
map2(., word.vectors.table[index$V1 %>% unique() %>% sort()], ~ .y[.x$V2])
推荐阅读
- reactjs - 使用 React 从 WP REST API 获取“标题”
- java - 将java源文件转为utf-8并将eclipse设置为utf-8后字符串显示问题
- javascript - 属性“转换”适用于某些元素,而不适用于其他元素 Chrome iOS
- php - 从 S3 获取“流不支持搜索”读取 CSV
- sql - SSRS 中的两个多值参数
- bash - 如何在bash中使用awk遍历字段的多个值?
- php - 找不到用于 Woocommerce 订单备注的操作挂钩
- node.js - 我部署的应用程序在桌面上运行良好,但在手机上是一个空白屏幕
- r - 从一列中选择一个值过滤另一列
- c# - 依赖注入在asp.net core web应用启动过程中注册类失败