【问题标题】:Error in R code for Reddit sentiment analysisReddit 情绪分析的 R 代码错误
【发布时间】:2022-01-11 18:45:55
【问题描述】:

我正在尝试在 R 中进行 Reddit 情绪分析。我无法克服错误,我正在尝试解决问题。

# Getting Reddit Data

links<-   find_thread_urls(
  keywords = "Ghostbusters",
  sort_by = "top",
  subreddit = NA,
  period = "all"
)

# function to iterate through all posts

funct = function(i){
  
  content = get_thread_content()(links$URL[i])
  
  com = iconv(content$comment, to = 'utf-8')
  
  clov = get_nrc_sentiment(com)
  
  x1 = 100*colSums(clov)/sum(clov)
  
  return(cbind(links[i,], t(x1) ))
  
}

# list of all the links

ls = 1:nrow(links)

# loop through all the links and bind to a data frame

res = do.call("rbind", lapply(ls, funct))

当我运行这段代码时,我得到了这个错误:

Error in lapply(urls, parse_thread_url) : 
argument "urls" is missing, with no default

我在这里错过了什么?

【问题讨论】:

  • 错误信息是不言自明的。函数parse_thread_url 需要一个参数url 而你还没有提供它。

标签: r sentiment-analysis reddit


【解决方案1】:

代码中几乎没有错别字,

links = find_thread_urls(
  keywords = "Ghostbusters",
  sort_by = "top",
  subreddit = NA,
  period = "all"
)
x0 = vector()
funct = function(i){
  content = get_thread_content(links$url[i])
  com = iconv(content$comment, to = 'utf-8')
  clov = get_nrc_sentiment(com)
  x1 = 100*colSums(clov)/sum(clov)
  x1 = cbind(x0, t(x1))
  return(x1)
}

ls = 1:nrow(links)
res = lapply(ls, funct)
res = do.call(rbind, res)
         anger anticipation  disgust     fear       joy  sadness surprise     trust negative positive
 [1,] 7.276119     8.955224 6.156716 9.514925  6.156716 9.328358 3.731343  9.701493 18.28358 20.89552
 [2,] 5.586592    11.731844 4.469274 6.145251 12.290503 8.379888 5.586592 14.525140 10.61453 20.67039
 [3,] 7.333333     9.238095 5.238095 8.952381  8.095238 6.571429 4.952381 11.523810 16.95238 21.14286
 [4,] 8.641975     6.790123 8.024691 7.098765  8.333333 8.024691 5.555556 11.111111 16.35802 20.06173

【讨论】:

  • 这行得通。我真的很感激!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 2018-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多