利用论坛上发布的资源,我设法创建了一个脚本,将文件转换为xts类的对象。但是,这是这些对象的列表。最终,我想将这些文件转换为单独的对象xts。我必须在脚本中输入一个更改,它们作为单独的对象出现,每个对象都是xts类?
有脚本:
files <- list.files( pattern="*.mst")
listmst <- vector("list", length(files))
names(listmst)<- files
for (i in 1:length(files)) {
temp <- read.zoo(files[i], sep=",", header=TRUE,
index.column=1, format="%Y%m%d", tz="",
colClasses = rep(c("NULL","character", "numeric"), c(1,1,5)))
listmst[[files[i]]]<- as.xts(temp)
}
rm(temp,files)
要转换的csv文件: https://www.dropbox.com/s/paql3y0gm5gve3z/pourek.rar?dl=0
答案 0 :(得分:0)
您需要使用类似assign
的内容来创建带有符号名称的对象。例如:
files <- list.files(pattern = "*.mst")
for (f in files) {
# Create symbol from file name
Symbol <- gsub("\\.mst", "", basename(f))
# Ensure it's a valid R name
Symbol <- make.names(Symbol)
# Read data from file
temp <- read.zoo(f, sep=",", header=TRUE, format="%Y%m%d", tz="",
colClasses = rep(c("NULL","character", "numeric"), c(1,1,5)))
# Clean up column names
colnames(temp) <- gsub("^X\\.|\\.$", "", colnames(temp))
# Create object named `symbol` containing data from `temp`
assign(Symbol, as.xts(temp))
}
rm(Symbol, temp, f)
现在您应该在工作区中看到这一点:
R> ls()
[1] "ABPL" "AILLERON" "ALIOR" "ALMA" "ALTA"
[6] "ALTERCO" "files" "KREDYTIN" "KREZUS" "KRKA"
[11] "KRUK" "KRUSZWICA" "MABION" "MAKARONPL" "MANGATA"
[16] "MARVIPOL" "MASTERPHA" "MBANK" "MBWS" "MCI"
[21] "MCLOGIC" "MDIENERGIA" "MEDIACAP" "MEDIATEL" "X11BIT"
[26] "X4FUNMEDIA"
R> head(ABPL)
OPEN HIGH LOW CLOSE VOL
2006-09-21 11.54 12.19 11.50 11.70 844641
2006-09-22 11.69 11.69 11.25 11.60 53033
2006-09-25 11.49 11.83 11.49 11.83 125607
2006-09-26 11.90 12.05 11.90 12.04 104956
2006-09-27 12.09 12.09 11.70 11.95 69062
2006-09-28 11.70 12.00 11.65 12.00 37466
请注意,以数字开头的所有符号现在都以“X”开头,因为以数字开头的R对象不是“语法上有效”。