我试图格式化一个列表,以便它可以被另一个函数解释。我列出了标记化单词的向量列表及其词性标记,如下所示:
> tokens
[[1]]
[1] "This" "is" "a" "test" "!"
[[2]]
[1] "This" "is" "a" "test" "!"
> pos_tags
[[1]]
[1] "DT" "VBZ" "DT" "NN" "."
[[2]]
[1] "DT" "VBZ" "DT" "NN" "."
我想将它们重新格式化为如下所示的列表。对于任何长度的列表,此过程应该是可重现的。
[[1]]
[[1]][[1]]
[[1]][[1]][[1]]
[1] "This"
[[1]][[1]][[2]]
[1] "DT"
[[1]][[2]]
[[1]][[2]][[1]]
[1] "is"
[[1]][[2]][[2]]
[1] "VBZ"
[[1]][[3]]
[[1]][[3]][[1]]
[1] "a"
[[1]][[3]][[2]]
[1] "DT"
[[1]][[4]]
[[1]][[4]][[1]]
[1] "test"
[[1]][[4]][[2]]
[1] "NN"
[[1]][[5]]
[[1]][[5]][[1]]
[1] "!"
[[1]][[5]][[2]]
[1] "."
[[2]]
[[2]][[1]]
[[2]][[1]][[1]]
[1] "This"
[[2]][[1]][[2]]
[1] "DT"
[[2]][[2]]
[[2]][[2]][[1]]
[1] "is"
[[2]][[2]][[2]]
[1] "VBZ"
[[2]][[3]]
[[2]][[3]][[1]]
[1] "a"
[[2]][[3]][[2]]
[1] "DT"
[[2]][[4]]
[[2]][[4]][[1]]
[1] "test"
[[2]][[4]][[2]]
[1] "NN"
[[2]][[5]]
[[2]][[5]][[1]]
[1] "!"
[[2]][[5]][[2]]
[1] "."
非常感谢任何指示。
答案 0 :(得分:1)
一种选择是在Map
R
Map(function(x, y) Map(list, as.list(x), as.list(y)), tokens, pos_tags)
#[[1]]
#[[1]][[1]]
#[[1]][[1]][[1]]
#[1] "This"
#[[1]][[1]][[2]]
#[1] "DT"
#[[1]][[2]]
#[[1]][[2]][[1]]
#[1] "is"
#[[1]][[2]][[2]]
#[1] "VBZ"
#[[1]][[3]]
#[[1]][[3]][[1]]
#[1] "a"
#[[1]][[3]][[2]]
#[1] "DT"
#[[1]][[4]]
#[[1]][[4]][[1]]
#[1] "test"
#[[1]][[4]][[2]]
#[1] "NN"
#[[1]][[5]]
#[[1]][[5]][[1]]
#[1] "!"
#[[1]][[5]][[2]]
#[1] "."
#[[2]]
#[[2]][[1]]
#[[2]][[1]][[1]]
#[1] "This"
#[[2]][[1]][[2]]
#[1] "DT"
#[[2]][[2]]
#[[2]][[2]][[1]]
#[1] "is"
#[[2]][[2]][[2]]
#[1] "VBZ"
#[[2]][[3]]
#[[2]][[3]][[1]]
#[1] "a"
#[[2]][[3]][[2]]
#[1] "DT"
#[[2]][[4]]
#[[2]][[4]][[1]]
#[1] "test"
#[[2]][[4]][[2]]
#[1] "NN"
#[[2]][[5]]
#[[2]][[5]][[1]]
#[1] "!"
#[[2]][[5]][[2]]
#[1] "."
tokens <- list(c("This", "is", "a", "test", "!"), c("This", "is", "a", "test", "!"))
pos_tags <- list(c("DT", "VBZ", "DT", "NN", "."), c("DT", "VBZ", "DT", "NN", "."))
答案 1 :(得分:1)
Map(function(x,y)as.list(c(rbind(x,y))),tokens,pos_tags)
[[1]]
[[1]][[1]]
[1] "This"
[[1]][[2]]
[1] "DT"
[[1]][[3]]
[1] "is"
[[1]][[4]]
[1] "VBZ"
[[1]][[5]]
[1] "a"
[[1]][[6]]
[1] "DT"
[[1]][[7]]
[1] "test"
[[1]][[8]]
[1] "NN"
[[1]][[9]]
[1] "!"
[[1]][[10]]
[1] "."
[[2]]
[[2]][[1]]
[1] "This"
[[2]][[2]]
[1] "DT"
[[2]][[3]]
[1] "is"
[[2]][[4]]
[1] "VBZ"
[[2]][[5]]
[1] "a"
[[2]][[6]]
[1] "DT"
[[2]][[7]]
[1] "test"
[[2]][[8]]
[1] "NN"
[[2]][[9]]
[1] "!"
[[2]][[10]]
[1] "."