以下是对此问题的跟进问题: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"))
答案 0 :(得分:0)
这是使用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])