而且& for循环保存所有结果

时间:2017-06-25 13:08:34

标签: r loops

嘿伙计们,我和while循环有问题,更确切地说,我需要弄清楚如何正确保存结果而不被最后一个覆盖,所以这是一个更普遍的问题,但我还没有找到任何问题在任何网站上解决这个问题。

中间部分的分析并不重要,因为它的工作。唯一的问题是每个企业后都会覆盖结果,我无法弄清楚如何保存包含所有企业的列表。我正在分析输出应该是的文本: positive_rating; negative_rating; positive_words; negative_words;字数;文件;每个四分之一的公司

目前,结果是“Adecco Grou AG”的所有四分之一的列表

我无法提供示例,但文本和词典由单列矩阵组成,每行包含一个文字。

我希望我能够清楚地表达自己,并提前感谢你的帮助!

library(readxl)
library(readr)
library(plyr)
library(dplyr)
library(xlsx)

path = "C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/Conference Calls/Text_formated/"


#Import Dictionary
positive_list <- read_excel("C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/LoughranMcDonald_MasterDictionary_2014.xlsx", 
                            sheet = "positive")
negative_list <- read_excel("C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/LoughranMcDonald_MasterDictionary_2014.xlsx", 
                            sheet = "negative")
#Initiate result list
positive_rating <- list()
negative_rating <- list()
word_count <- list()
positive_words <- list()
negative_words <- list()

Files <- c(  "2011 Q1.csv","2011 Q2.csv","2011 Q3.csv","2011 Q4.csv",
             "2012 Q1.csv","2012 Q2.csv","2012 Q3.csv","2012 Q4.csv",
             "2013 Q1.csv","2013 Q2.csv","2013 Q3.csv","2013 Q4.csv",
             "2014 Q1.csv","2014 Q2.csv","2014 Q3.csv","2014 Q4.csv",
             "2015 Q1.csv","2015 Q2.csv","2015 Q3.csv","2015 Q4.csv",
             "2016 Q1.csv","2016 Q2.csv","2016 Q3.csv","2016 Q4.csv")
Enterprise <- c("ABB Ltd","Actelion Ltd", "Adecco Group AG")
g1 <- length(Enterprise)
company <- as.integer(1)

while(company <= g1){
  for (i in 1:length(Files))
  { 
    ##**ANALYSIS**##

  firm <- Enterprise[company]

  }
company <- company+1
}

#Add Identifier to list
Rating_list =cbind(positive_rating,negative_rating,positive_words,negative_words,word_count,Files, firm)

View(Rating_list)

以防我将所有代码留在最后,如果需要的话。

library(readxl)
library(readr)
library(plyr)
library(dplyr)
library(xlsx)

path = "C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/Conference Calls/Text_formated/"


#Import Dictionary
positive_list <- read_excel("C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/LoughranMcDonald_MasterDictionary_2014.xlsx", 
                            sheet = "positive")
negative_list <- read_excel("C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/LoughranMcDonald_MasterDictionary_2014.xlsx", 
                            sheet = "negative")
#Initiate result list
positive_rating <- list()
negative_rating <- list()
word_count <- list()
positive_words <- list()
negative_words <- list()

Files <- c(  "2011 Q1.csv","2011 Q2.csv","2011 Q3.csv","2011 Q4.csv",
             "2012 Q1.csv","2012 Q2.csv","2012 Q3.csv","2012 Q4.csv",
             "2013 Q1.csv","2013 Q2.csv","2013 Q3.csv","2013 Q4.csv",
             "2014 Q1.csv","2014 Q2.csv","2014 Q3.csv","2014 Q4.csv",
             "2015 Q1.csv","2015 Q2.csv","2015 Q3.csv","2015 Q4.csv",
             "2016 Q1.csv","2016 Q2.csv","2016 Q3.csv","2016 Q4.csv")
Enterprise <- c("ABB Ltd","Actelion Ltd", "Adecco Group AG")
g1 <- length(Enterprise)
company <- as.integer(1)

while(company <= g1){
  for (i in 1:length(Files))
  { 
    #Import Text
    Text <- read_delim(paste0(path,Enterprise[company],"/",Files[i]), 
                       ";", escape_double = FALSE, col_names = FALSE, 
                       trim_ws = TRUE)
    #Formating ->  Transp, Vector, small, dataframe
    Text = data.frame(tolower(c(t(Text))))
    colnames(Text) = "Word"
    #Joining Text-Dictionary
    positive_rate = inner_join(positive_list,Text)
    #Analysis
    positive_rate$count <- 1
    positive_rating[[i]] = sum(positive_rate$Rating) 
    positive_words[[i]] = sum(positive_rate$count)

    #Import Text
    Text <- read_delim(paste0(path,Enterprise[company],"/",Files[i]), 
                       ";", escape_double = FALSE, col_names = FALSE, 
                       trim_ws = TRUE)
    #Formating ->  Transp, Vector, small, dataframe
    Text = data.frame(tolower(c(t(Text))))
    colnames(Text) = "Word"
    #negative part
    negative_rate = inner_join(negative_list,Text)
    #Analysis
    negative_rate$count <- 1
    negative_rating[[i]] = sum(negative_rate$Rating)
    negative_words[[i]] = sum(negative_rate$count)

    #Import Text
    Text <- read_delim(paste0(path,Enterprise[company],"/",Files[i]), 
                       ";", escape_double = FALSE, col_names = FALSE, 
                       trim_ws = TRUE)
    #Formating ->  Transp, Vector, small, dataframe
    Text = data.frame(tolower(c(t(Text))))
    colnames(Text) = "Word"
    #Word counting 
    Text <- na.omit(Text)
    Text$count <- 1
    word_count[[i]] = sum(Text$count)

  firm <- Enterprise[company]

  }
company <- company+1
}

#Add Identifier to list
Rating_list =cbind(positive_rating,negative_rating,positive_words,negative_words,word_count,Files, firm)


View(Rating_list)  

0 个答案:

没有答案