我正在尝试使用udpipe的RAKE在数据帧中为每个文档生成一个包含25个RAKE标记的列表,并将这些标记(加上一个简单的str_count)写回数据帧。我构造了一个for循环来处理,但是我将相同的结果写入每一行,而不是将不同的结果写入每一行。
安装和使用的包有udpipe、dplyr、stringi、stringr、data.table。
annotation$length <- nchar(annotation$token)
annotation <- annotation %>% filter(length >= 3 )
counter <- textdf$doc_id
for (i in counter) {
subannotation <- annotation %>% filter(doc_id == i)
stats <-
keywords_rake(
x = subannotation,
term = "token", #token or lemma
group = "doc_id",
ngram_max = 3,
n_min = 1,
relevant = subannotation$upos %in% c("NOUN", "VERB", "ADV", "ADJ")
)
stats <- stats %>% top_n(25,rake)
checktopics <- paste(stats$keyword, collapse = " ")
textdf$topics <- checktopics
textdf$score <- str_count(checktopics,"cheese")
}
预期的结果应该是这样的:
id score topics
1 12 chocolate chocoholics cheese
2 1 plastic waste cheese
3 3 neuroscientists data system
目前的结果是:
id score topics
1 3 neuroscientists data system
2 3 neuroscientists data system
3 3 neuroscientists data system
我做错了什么?
谢谢!
发布于 2019-02-10 20:44:59
适当的修复方法是将指针添加到循环中的行。德尔普。
textdf$topics[i] <- checktopics
textdf$score[i] <- str_count(checktopics,"cheese")
https://stackoverflow.com/questions/54612390
复制相似问题