通过在for循环中使用索引,将值迭代地添加到列表中
这是这个问题的后续问题: 我希望以非常特定的方式向列表中迭代添加值: 我有以下清单:通过在for循环中使用索引,将值迭代地添加到列表中,r,list,for-loop,R,List,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,
> 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)表示列表中组件的编号,第二对表示组件x中的y值
> 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]])
}
下面是一个使用
tidyverse
包套件的解决方案。首先,我将更改索引向量的格式:
index <- do.call(rbind, index) %>%
as_data_frame()
你能举个例子说明你的输出应该是什么样的吗?谢谢@MelissaKey我刚刚添加了所需的输出!基本上,我想要一个包含几个组件的列表,这些组件是单词计数的
table
输出
index <- do.call(rbind, index) %>%
as_data_frame()
result <- result <- index %>%
split(., .$V1) %>%
map2(., word.vectors.table[index$V1 %>% unique() %>% sort()], ~ .y[.x$V2])