【问题标题】:Object of type 'closure' is not subsettable - R“闭包”类型的对象不可子集 - R
【发布时间】:2016-04-14 16:43:54
【问题描述】:

我正在使用 R 提取推文并分析他们的情绪,但是当我到达下面的行时,我收到一条错误消息“'closure' 类型的对象不是子集的”

scores$drink = factor(rep(c("east"), nd))
scores$very.pos = as.numeric(scores$score >= 2)
scores$very.neg = as.numeric(scores$score <= -2)

下面粘贴完整代码

load("twitCred.Rdata")
east_tweets <- filterStream("tweetselnd.json", locations = c(-0.10444, 51.408699, 0.33403, 51.64661),timeout = 120, oauth = twitCred)
tweets.df <- parseTweets("tweetselnd.json", verbose = FALSE)

 ##function score.sentiment

score.sentiment = function(sentences, pos.words, neg.words, .progress='none')
{
  # Parameters
  # sentences: vector of text to score
  # pos.words: vector of words of postive sentiment
  # neg.words: vector of words of negative sentiment
  # .progress: passed to laply() to control of progress bar

  scores = laply(sentences,
                 function(sentence, pos.words, neg.words)
                 {
                   # remove punctuation
                   sentence = gsub("[[:punct:]]", "", sentence)
                   # remove control characters
                   sentence = gsub("[[:cntrl:]]", "", sentence)
                   # remove digits?
                   sentence = gsub('\\d+', '', sentence)

                   # define error handling function when trying tolower
                   tryTolower = function(x)
                   {
                     # create missing value
                     y = NA
                     # tryCatch error
                     try_error = tryCatch(tolower(x), error=function(e) e)
                     # if not an error
                     if (!inherits(try_error, "error"))
                       y = tolower(x)
                     # result
                     return(y)
                   }
                   # use tryTolower with sapply 
                   sentence = sapply(sentence, tryTolower)

                   # split sentence into words with str_split (stringr package)
                   word.list = str_split(sentence, "\\s+")
                   words = unlist(word.list)

                   # compare words to the dictionaries of positive & negative terms
                   pos.matches = match(words, pos.words)
                   neg.matches = match(words, neg.words)

                   # get the position of the matched term or NA
                   # we just want a TRUE/FALSE
                   pos.matches = !is.na(pos.matches)
                   neg.matches = !is.na(neg.matches)

                   # final score
                   score = sum(pos.matches) - sum(neg.matches)
                   return(score)
                 }, pos.words, neg.words, .progress=.progress )

  # data frame with scores for each sentence
  scores.df = data.frame(text=sentences, score=scores)
  return(scores.df)
}

pos = readLines(file.choose())
neg = readLines(file.choose())

east_text = sapply(east_tweets, function(x) x$getText())

scores = score.sentiment(tweetseldn.json, pos, neg, .progress='text')

scores()$drink = factor(rep(c("east"), nd))
scores()$very.pos = as.numeric(scores()$score >= 2)
scores$very.neg = as.numeric(scores$score <= -2)

# how many very positives and very negatives
numpos = sum(scores$very.pos)
numneg = sum(scores$very.neg)

# global score
global_score = round( 100 * numpos / (numpos + numneg) )

如果有人可以帮助我了解为什么会出现此错误,我们将不胜感激。此外,我还看到了其他关于在引用变量“分数”时添加“()”的答案,例如分数()$....但它对我不起作用。谢谢。

【问题讨论】:

  • 如果您发布完整的错误,我们有机会猜出它的来源。如果没有,请查看stackoverflow.com/q/11308367
  • FULL ERROR : score$drink = factor(rep(c("east"), nd)) 中的错误:'closure' 类型的对象不是子集
  • 我也有同样的想法,但是当我尝试时,出现以下错误: 分数错误()$drink = factor(rep(c("east_text"), nd)) : invalid (NULL)作业左侧
  • 哦,我现在明白了。也许分配分数的结果,如x &lt;- scores(),然后分配如x$drink &lt;- stuff
  • 在尝试运行 x

标签: json r twitter


【解决方案1】:

以下更改消除了错误:

x <- scores
x$drink = factor(rep(c("east"), nd))
x$very.pos = as.numeric(x$score >= 2)
x$very.neg = as.numeric(x$score <= -2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 2018-06-22
    • 1970-01-01
    • 2023-03-27
    • 2014-02-23
    • 2019-03-23
    相关资源
    最近更新 更多