在循环中使用TmParallelApply创建变量

问题描述

我正在尝试压缩以下代码块:

for (i in 1:nrow(Bernie_Sentiments)) {

nrctoken <- data_frame(text = Bernie_Sentiments$text[i]) %>% unnest_tokens(word,text) %>% anti_join(stop_words) %>% inner_join(get_sentiments("nrc")) %>% count(sentiment) %>% spread(sentiment,n,fill = 0)

nrc_positive = tryCatch(nrctoken$positive,warning = function(x) 0,error = function(x) 0)
nrc_negative = tryCatch(nrctoken$negative,error = function(x) 0)
nrc_anger = tryCatch(nrctoken$anger,error = function(x) 0)
nrc_anticipation = tryCatch(nrctoken$anticipation,error = function(x) 0)
nrc_disgust = tryCatch(nrctoken$disgust,error = function(x) 0)
nrc_fear = tryCatch(nrctoken$fear,error = function(x) 0)
nrc_joy = tryCatch(nrctoken$joy,error = function(x) 0)
nrc_sadness = tryCatch(nrctoken$sadness,error = function(x) 0)
nrc_surprise = tryCatch(nrctoken$surprise,error = function(x) 0)
nrc_trust = tryCatch(nrctoken$trust,error = function(x) 0)

Bernie_Sentiments$nrc_positive[i] <- nrc_positive
Bernie_Sentiments$nrc_negative[i] <- nrc_negative
Bernie_Sentiments$nrc_anger[i] <- nrc_anger
Bernie_Sentiments$nrc_anticipation[i] <- nrc_anticipation
Bernie_Sentiments$nrc_disgust[i] <- nrc_disgust
Bernie_Sentiments$nrc_fear[i] <- nrc_fear
Bernie_Sentiments$nrc_joy[i] <- nrc_joy
Bernie_Sentiments$nrc_sadness[i] <- nrc_sadness
Bernie_Sentiments$nrc_surprise[i] <- nrc_surprise 
Bernie_Sentiments$nrc_trust[i] <- nrc_trust
}

我想出了这个:

for (i in 1:nrow(Bernie_Sentiments)) {

 nrctoken <- data_frame(text = Bernie_Sentiments$text[i]) 
 %>% unnest_tokens(word,text) %>% anti_join(stop_words) 
 %>% inner_join(get_sentiments("nrc")) %>% 
 count(sentiment) %>% spread(sentiment,fill = 0) 
 emotions_list <- c('positive','negative','anger','anticipation','disgust','fear','joy','sadness','surprise','trust')

 TmParallelApply(X = emotions_list,FUN = function(x) 
 {substitute(x,Bernie_Sentiments)[i] = 
 tryCatch(substitute(x,nrctoken),warning = function(x) 
 0,error = function(x) 0)},export = ls(),envir = 
 environment())
}

但是,我不断收到错误消息:checkForRemoteErrors(val)中的错误:8个节点产生了错误;第一个错误:“符号”类型的对象不可子集化。任何有关如何修正或改进我的代码的想法将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)