R:遍历句子中的单词,并给出某个单词在句子中的位置

R: iterate over words in sentence, and give position of a certain word in sentence

它可以迭代单词,但变量“单词”包含一个单词,而不是该单词在行中的编号(位置)。例如,在第一行中,'yzi' 的编号为 1,'runner' 的编号为 3。有人可以帮忙吗?

你在找这个吗?

lapply(strsplit(output$text, ' '), function(x) seq_along(x)^2)

#[[1]]
# [1]   1   4   9  16  25  36  49  64  81 100 121 144 169 196

#[[2]]
# [1]   1   4   9  16  25  36  49  64  81 100 121 144 169 196

#[[3]]
# [1]   1   4   9  16  25  36  49  64  81 100 121 144 169

#[[4]]
# [1]   1   4   9  16  25  36  49  64  81 100

#...
#...

或者循环 -

for(row in 1:nrow(output)){
  list=strsplit(output$text[row], " ")[[1]]
  for(i in seq_along(list)){
    print(i^2)
  }
}

我们可以使用map

library(purrr)
map(strsplit(output$text, ' '), ~ seq_along(.x)^2)