0

I'm trying to load data from excel (from multiple files) into R using the xlsx package und converting the data into a xts object. The data should get as variable names the names of the related .xlsx sheet. The data has in the first column the dates and in the second a price.

My code so far:

 path<-"C:/test/"
 files<-list.files(path=path)
 j<-1
 for (i in files){
 name<-strsplit(i,'[.]')[[1]][1]
 assign(name,read.xlsx(file=paste(path,i,collapse=NULL,sep=""),sheetIndex=1,header=TRUE,as.data.frame=TRUE))
 files[j]<-name
 j<-j+1
 }

Now I want to change the type to a xts object. But I don't know how to deal with the dates. One solution I found is to assign the first column as rowname but I don't know how to implement this without losing the variable names.

I would appreciate your help. Thx

rainer
  • 929
  • 2
  • 14
  • 25

1 Answers1

1

Let's assume, that "name" is variable, which you want to convert into xts (it can be matrix, data.frame and etc.) and first column is date column such as "99/01/01", then conversion would be:

result=xts(nasa[,-1],order.by=as.POSIXct(strptime(a[,1],'%y/%d/%m')))

To set colnames, you can do:

colnames(result)=colnames(name)
Dzidas
  • 305
  • 4
  • 13