Constantine Tsardounis
2005-Nov-11 14:07 UTC
[R] Inputing data from multiple files as time series objects
Hello to everyone,... I am a new R ambitious user. I would like to be the first at my department using R, but I have encountered a difficulty during the last days that I cannot overcome reading help() and searching over the net. Problem: I have multiple files with financial data like the following (header included): E.g.: filename: AOL.txt aol.txt 4 3 5 3... filename: IBM.txt ibm.txt 6 2 5 2... I would like to input these data in R as time-series objects with their corresponding names automatically, so that I can manipulate them (exempli gratia plot acf, pacf, differntiate them, make adf tests, etc) For example: I could do that by hand using: AOL <- ts(read.table(AOL.txt, header = TRUE)))) IBM <- ts(read.table(IBM.txt, header = TRUE)))) but is there another way to achieve the same actions as above with a more versatile way? (for example loops?) Or would you suggest inputing these data with another way or as other objects? Thank you very much in advance,... Tsardounis Costas
Sean Davis
2005-Nov-11 14:24 UTC
[R] Inputing data from multiple files as time series objects
On 11/11/05 9:07 AM, "Constantine Tsardounis" <costas.magnuse at gmail.com> wrote:> Hello to everyone,... > > I am a new R ambitious user. I would like to be the first at my > department using R, but I have encountered a difficulty during the > last days that I cannot overcome reading help() and searching over the > net. > Problem: > I have multiple files with financial data like the following (header > included): > E.g.: > filename: AOL.txt > aol.txt > 4 > 3 > 5 > 3... > > filename: IBM.txt > ibm.txt > 6 > 2 > 5 > 2... > > I would like to input these data in R as time-series objects with > their corresponding names automatically, so that I can manipulate them > (exempli gratia plot acf, pacf, differntiate them, make adf tests, > etc) > For example: I could do that by hand using: > AOL <- ts(read.table(AOL.txt, header = TRUE)))) > IBM <- ts(read.table(IBM.txt, header = TRUE)))) > but is there another way to achieve the same actions as above with a > more versatile way? (for example loops?) > Or would you suggest inputing these data with another way or as other objects?myts <- list() mydir <- 'path/to/files' # put all files in the same directory myfiles <- dir('path/to/files',pattern='.txt') for (i in myfiles) { myts[[sub('.txt','',i)]] <- ts(read.table(paste(mydir,myfiles,sep='/'),header=T)) } This will give you back a list of ts objects based on all txt files in a directory. I haven't tested the code, but I hope you get the idea. Sean
Michaell Taylor
2005-Nov-11 14:41 UTC
[R] Inputing data from multiple files as time series objects
Assuming all the data are in a subdirectory called "Data" and all the files have the '.txt' extension, you could do something like. files <- list.files('Data') for (file in files){ temp <- ts(read.table(file, header = T)) vname <- sub('.txt','',file) assign(vname,temp,envir=.Globalenv) } On Friday 11 November 2005 08:07 am, Constantine Tsardounis wrote:> Hello to everyone,... > > I am a new R ambitious user. I would like to be the first at my > department using R, but I have encountered a difficulty during the > last days that I cannot overcome reading help() and searching over the > net. > Problem: > I have multiple files with financial data like the following (header > included): E.g.: > filename: AOL.txt > aol.txt > 4 > 3 > 5 > 3... > > filename: IBM.txt > ibm.txt > 6 > 2 > 5 > 2... > > I would like to input these data in R as time-series objects with > their corresponding names automatically, so that I can manipulate them > (exempli gratia plot acf, pacf, differntiate them, make adf tests, > etc) > For example: I could do that by hand using: > AOL <- ts(read.table(AOL.txt, header = TRUE)))) > IBM <- ts(read.table(IBM.txt, header = TRUE)))) > but is there another way to achieve the same actions as above with a > more versatile way? (for example loops?) > Or would you suggest inputing these data with another way or as other > objects? > > Thank you very much in advance,... > > Tsardounis Costas > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html-- ======================================Michaell Taylor, PhD. Principal Boxwood Means, Inc. 203.653.4100
ManuelPerera-Chang@fmc-ag.com
2005-Nov-11 15:24 UTC
[R] Inputing data from multiple files as time series objects
Hi Constantine, I have not tested it but try ... y<-c("IBM","MS","DELL","SIEMENS","SUN") read.data.myfunction<-function(x){ for(i in 1:length(x)) { paste(x[i],sep="") <- ts(read.table(paste(x[i],".txt",sep=""), header = TRUE)) } } read.data.myfunction(y) Manuel Constantine Tsardounis To: r-help at stat.math.ethz.ch <costas.magnuse at gmail cc: .com> Subject: [R] Inputing data from multiple files as time series objects Sent by: r-help-bounces at stat.m ath.ethz.ch 11.11.2005 15:07 Hello to everyone,... I am a new R ambitious user. I would like to be the first at my department using R, but I have encountered a difficulty during the last days that I cannot overcome reading help() and searching over the net. Problem: I have multiple files with financial data like the following (header included): E.g.: filename: AOL.txt aol.txt 4 3 5 3... filename: IBM.txt ibm.txt 6 2 5 2... I would like to input these data in R as time-series objects with their corresponding names automatically, so that I can manipulate them (exempli gratia plot acf, pacf, differntiate them, make adf tests, etc) For example: I could do that by hand using: AOL <- ts(read.table(AOL.txt, header = TRUE)))) IBM <- ts(read.table(IBM.txt, header = TRUE)))) but is there another way to achieve the same actions as above with a more versatile way? (for example loops?) Or would you suggest inputing these data with another way or as other objects? Thank you very much in advance,... Tsardounis Costas ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html