gpt4 book ai didi

r - 参数列数不匹配

转载 作者:行者123 更新时间:2023-12-03 22:15:57 25 4
gpt4 key购买 nike

我正在使用这个 example在R中对一组txt文档进行情感分析,代码为:

library(tm)
library(tidyverse)
library(tidytext)
library(glue)
library(stringr)
library(dplyr)
library(wordcloud)
require(reshape2)

files <- list.files(inputdir,pattern="*.txt")

GetNrcSentiment <- function(file){

fileName <- glue(inputdir, file, sep = "")
fileName <- trimws(fileName)
fileText <- glue(read_file(fileName))
fileText <- gsub("\\$", "", fileText)

tokens <- data_frame(text = fileText) %>% unnest_tokens(word, text)

# get the sentiment from the first text:
sentiment <- tokens %>%
inner_join(get_sentiments("nrc")) %>% # pull out only sentiment words
count(sentiment) %>% # count the # of positive & negative words
spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
mutate(sentiment = positive - negative) %>% # positive - negative
mutate(file = file) %>% # add the name of our file
mutate(year = as.numeric(str_match(file, "\\d{4}"))) %>% # add the year
mutate(city = str_match(file, "(.*?).2")[2])

return(sentiment)
}

.txt 文件存储在 inputdir并有姓名 AB-City.0000 ,其中 AB 是国家的缩写,City 是城市名称,0000 是年份(范围从 2000 年到 2017 年)。

该函数按预期适用于单个文件,即 GetNrcSentiment(files[1])给我一点点,每种情绪都有适当的计数。但是,当我尝试为整套运行它时,即
nrc_sentiments  <- data_frame()

for(i in files){
nrc_sentiments <- rbind(nrc_sentiments, GetNrcSentiment(i))
}

我收到以下错误消息:
Joining, by = "word"
Error in rbind(deparse.level, ...) :
numbers of columns of arguments do not match

完全相同的代码适用于较长的文档,但在处理较短的文本时会出错。似乎并非所有情绪都在小文档中找到,因此每个文档的列数各不相同,这可能会导致此错误,但我不确定。我将不胜感激有关如何解决问题的任何建议。如果没有找到情绪,我希望条目等于零(如果这是我的问题的原因)。

顺便说一句,bing 情绪函数运行了大约两打文件并给出了不同的错误,这似乎指向了同样的问题(未找到负面情绪?):
GetBingSentiment <- function(file){
fileName <- glue(inputdir, file, sep = "")
fileName <- trimws(fileName)

fileText <- glue(read_file(fileName))
fileText <- gsub("\\$", "", fileText)
tokens <- data_frame(text = fileText) %>% unnest_tokens(word, text)

# get the sentiment from the first text:
sentiment <- tokens %>%
inner_join(get_sentiments("bing")) %>% # pull out only sentiment words
count(sentiment) %>% # count the # of positive & negative words
spread(sentiment, n, fill = 0) %>% # made data wide rather than narrow
mutate(sentiment = positive - negative) %>%
mutate(file = file) %>% # add the name of our file
mutate(year = as.numeric(str_match(file, "\\d{4}"))) %>% # add the year
mutate(city = str_match(file, "(.*?).2")[2])

# return our sentiment dataframe
return(sentiment)
}

Error in mutate_impl(.data, dots) :
Evaluation error: object 'negative' not found.

编辑:按照 David Klotz 的建议,我将代码编辑为
for(i in files){ nrc_sentiments <- dplyr::bind_rows(nrc_sentiments, GetNrcSentiment(i)) } 

结果,如果未找到来自某种情绪的词,nrc 不会抛出错误,而是生成 NA,但是在 22 次加入后,我得到了一个不同的错误:
Error in mutate_impl(.data, dots) : Evaluation error: object 'negative' not found.
使用 dplyr 运行 bing 函数时出现相同的错误。当函数到达第 22 个文档时,两个数据框都包含所有情绪的列。什么可能导致错误以及如何诊断它?

最佳答案

dplyr bind_rows功能比rbind更灵活,至少在缺少列时:

nrc_sentiments <- dplyr::bind_rows(nrc_sentiments, GetNrcSentiment(i))

关于r - 参数列数不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50821024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com