将每个数据帧保存到每个数据时出现问题

时间:2019-10-13 16:30:03

标签: r computer-science data-science data-analysis

我必须在2006年至2015年之间四分之一地分析基金的市场价值,我必须区分某个国家和其他国家(子集),并且我有一堆txt文件像2006-03-31、2006-06-30、2006-09-30、2006-12-31 ... 2015-12-31。

但是当这样做40次时,出现以下错误:

  

文件错误(文件,“ rt”):无法打开连接

     

此外:警告消息:   在file(file,“ rt”)中:     无法打开文件'C:/ Users / Ilja Ivanov / Desktop / Lab 3,2006-03-31.txt':没有此类文件或目录

library(ggplot2)
library(dplyr)

file.exists("C:/Users/Ilja Ivanov/Desktop/Lab 3,2006-03-31.txt")

#Sets working directory 
setwd("C:/Users/Ilja Ivanov/Desktop/Lab 3")
#====================================================================================

#Sets the path of the project where to look for the files
mypath <- "C:/Users/Ilja Ivanov/Desktop/Lab 3"

#====================================================================================

#Reads files from path into "file.names"
file.names <- dir(mypath, pattern =".txt")

#====================================================================================

#Creates an empty data object to store each
#of the imported file in a sorted fashion
files <-sort(file.names)

#====================================================================================

# To store the final data
out.file <- NULL

#====================================================================================

#Looping through the files, concatenating and separating
#by string. All data then merged by rows to "out.file"
#Conditionals for saving each data frame to each data from 1 to 40
for (i in 1:length(files)) 
{
  path <- paste(mypath,files[i], sep = ",")
  out.file <-rbind(out.file, mypath)
  if(i==1)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    one<-cbind(data,quarter,year)
  } 
  if(i==2)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    two<-cbind(data,quarter,year)
  } 
  if(i==3)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    three<-cbind(data,quarter,year)
  } 
  if(i==4)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    four<-cbind(data,quarter,year)

    y2006<-rbind(one,two,three,four)
  }
  if(i==5)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    five<-cbind(data,quarter,year)
  } 
  if(i==6)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    six<-cbind(data,quarter,year)
  } 
  if(i==7)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    seven<-cbind(data,quarter,year)
  } 
  if(i==8)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    eight<-cbind(data,quarter,year)

    y2007<-rbind(five,six,seven,eight)
  }
  if(i==9)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    nine<-cbind(data,quarter,year)
  } 
  if(i==10)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    ten<-cbind(data,quarter,year)
  } 
  if(i==11)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    eleven<-cbind(data,quarter,year)
  } 
  if(i==12)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twelve<-cbind(data,quarter,year)

    y2008<-rbind(nine,ten,eleven,twelve)
  }
  if(i==13)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirteen<-cbind(data,quarter,year)
  } 
  if(i==14)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    fourteen<-cbind(data,quarter,year)
  } 
  if(i==15)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    fifteen<-cbind(data,quarter,year)
  } 
  if(i==16)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    sixteen<-cbind(data,quarter,year)

    y2009<-rbind(thirteen,fourteen,fifteen,sixteen)
  }
  if(i==17)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    seventeen<-cbind(data,quarter,year)
  } 
  if(i==18)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    eighteen<-cbind(data,quarter,year)
  } 
  if(i==19)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    nineteen<-cbind(data,quarter,year)
  } 
  if(i==20)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twenty<-cbind(data,quarter,year)

    y2010<-rbind(seventeen,eighteen,nineteen,twenty)
  }
  if(i==21)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentyone<-cbind(data,quarter,year)
  } 
  if(i==22)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentytwo<-cbind(data,quarter,year)
  } 
  if(i==23)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentythree<-cbind(data,quarter,year)
  } 
  if(i==24)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentyfour<-cbind(data,quarter,year)

    y2011<-rbind(twentyone,twentytwo,twentythree,twentyfour)
  }
  if(i==25)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentyfive<-cbind(data,quarter,year)
  } 
  if(i==26)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentysix<-cbind(data,quarter,year)
  } 
  if(i==27)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentyseven<-cbind(data,quarter,year)
  } 
  if(i==28)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    four<-cbind(data,quarter,year)

    y2012<-rbind(twentyfive,twentysix,twentyseven,twentyeight)
  }
  if(i==29)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    twentynine<-cbind(data,quarter,year)
  } 
  if(i==30)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirty<-cbind(data,quarter,year)
  } 
  if(i==31)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtyone<-cbind(data,quarter,year)
  } 
  if(i==32)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtytwo<-cbind(data,quarter,year)

    y2013<-rbind(twentynince,thirty,thirtyone,thirtytwo)
  }
  if(i==33)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtythree<-cbind(data,quarter,year)
  } 
  if(i==34)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtyfour<-cbind(data,quarter,year)
  } 
  if(i==35)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtyfive<-cbind(data,quarter,year)
  } 
  if(i==36)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtysix<-cbind(data,quarter,year)

    y2014<-rbind(thirtythree,thirtyfour,thirtyfive,thirtysix)
  }
  if(i==37)
  {
    data<-data.frame(read.delim2(path,quote = "",stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtyseven<-cbind(data,quarter,year)
  } 
  if(i==38)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtyeight<-cbind(data,quarter,year)
  } 
  if(i==39)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    thirtynine<-cbind(data,quarter,year)
  } 
  if(i==40)
  {
    data<-data.frame(read.delim2(path,stringsAsFactors = TRUE))
    qq<-rep(substr(files[i],6,7),1,length(data$Institutnr_fond))
    yy<-rep(substr(files[i],1,4),1,length(data$Institutnr_fond))
    fourty<-cbind(data,quarter,year)

    y2015<-rbind(thirtyseven,thirtyeight,thirtynine,fourty)
  }

}

all_Data <-rbind(y2006,y2007,y2008,y2009,y2010,y2011,y2012,y2013,y2014,y2015)

该文件显然存在,我尝试以管理员身份运行R,并使用“ file.exists”功能进行检查,结果结果为false。我如何解决这个问题,同时又有另一种更好的方法编写此脚本而不必产生此脚本墙?

0 个答案:

没有答案