嗨,我的循环不起作用。它只需要最后一个变量。这是代码:
library(readxl)
library(readr)
library(plyr)
library(dplyr)
path = "C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/Conference Calls/"
Enterprise = "ABB Ltd"
#Import Dictionary
Dictionary <- read_excel("C:/Users/benja/OneDrive/Studium/Bachelorarbeit/Ressourcen/LoughranMcDonald_MasterDictionary_2014.xlsx",
sheet = "Tabelle1")
for (File in c("2016 Q1.xml","2016 Q2.xml","2016 Q3.xml","2016 Q4.txt"))
{
#Import Text
ABB_2016_Q4 <- read_delim(paste0(path,Enterprise,"/",File),
" ", escape_double = FALSE, col_names = FALSE,
trim_ws = TRUE)
#Umformatierung -> Zuerst Transp, Vektor, kleinbuchstaben, dataframe
ABB_2016_Q4 = data.frame(tolower(c(t(ABB_2016_Q4))))
colnames(ABB_2016_Q4) = "Word"
#Zusammenführung Text-Dictionary
Analyze_2016_Q4 = inner_join(Dictionary,ABB_2016_Q4)
#Analyse
Rating = sum(Analyze_2016_Q4$Rating)
}
如果我尝试用
进行测试 print(File)
它有适当的列表,但循环不起作用。如何在每次循环后保存结果? 我希望显示不同四分位数的每个评级。
答案 0 :(得分:0)
你的循环可能正常,但目前它没有返回任何东西:)
您可以将结果写入列表:
#initiate result list
allResults <- list()
#populate your filelist; depending on your directory, you can also use list.files()
files <- c("2016 Q1.xml","2016 Q2.xml","2016 Q3.xml","2016 Q4.txt")
#iterate through your files
for (i in (1:length(files))
{ #Import Text
ABB_2016_Q4 <- read_delim(paste0(path,Enterprise,"/",files[i]),
" ", escape_double = FALSE, col_names = FALSE,
trim_ws = TRUE)
#Umformatierung -> Zuerst Transp, Vektor, kleinbuchstaben, dataframe
ABB_2016_Q4 = data.frame(tolower(c(t(ABB_2016_Q4))))
colnames(ABB_2016_Q4) = "Word"
#Zusammenführung Text-Dictionary
Analyze_2016_Q4 = inner_join(Dictionary,ABB_2016_Q4)
#Analyse & store results & add identifier:
allResults[[i]] = data.frame(ID = paste0("Q",i),
result =sum(Analyze_2016_Q4$Rating),
stringsAsFactors = FALSE)
}
# flatten resultlist to a dataframe:
allResultsDf <- do.call(rbind, allResults)
答案 1 :(得分:0)
看起来您正在加载一个“主”文件,然后加载大量单个文件并尝试将这些文件加入到主文件中。如果是这种情况,我会采用更实用的方法,而不是使用for()
循环。
一些示例数据:
master <- data.frame(
key = letters,
stringsAsFactors = FALSE
)
a <- data.frame(
key = sample(letters, 13),
dat = sample(1:100, 13),
stringsAsFactors = FALSE
)
a$key
letters_reduced <- letters %in% a$key
letters_reduced <- letters[!letters_reduced]
b <- data.frame(
key = sample(letters_reduced, 13),
dat = sample(1:100, 13),
stringsAsFactors = FALSE
)
readr::write_csv(a, "~/StackOverflow/BenjaminBerger/a.csv")
readr::write_csv(b, "~/StackOverflow/BenjaminBerger/b.csv")
所以我们在内存中有主对象。要加载R中的多个文件,假设它们位于同一目录中,我会使用list.files()
然后使用lapply()
和read_csv()
迭代文件:
files <- list.files("StackOverflow/BenjaminBerger", pattern = "*.csv",
full.names = TRUE)
df <- lapply(files, readr::read_csv)
您现在拥有一个数据框列表。您可以通过多种方式将这些方法连接到主对象,但最简单的方法是将数据框列表“折叠”为一个数据框,然后使用此方法进行连接。这很简单:
df <- dplyr::bind_rows(df)
master <- dplyr::inner_join(master, df, by = "key")
哪个可以帮到你:
head(master)
# key dat
# 1 a 38
# 2 b 52
# 3 c 59
# 4 d 77
# 5 e 34
# 6 f 93