(1)从文件中 read.zoo可以将文件名的字符向量作为其第一个参数,这样:
# create test files
Lines <- '"Index","pp"
1951-01-01,22.9
1951-01-02,4.3
1951-01-03,4.6'
cat(Lines, file = "testzoo01.csv")
cat(Lines, file = "testzoo02.csv")
# read.zoo reads the files named in Filenames and merges them
library(zoo)
Filenames <- dir(pattern = "testzoo.*csv")
z <- read.zoo(Filenames, sep = ",", header = TRUE)
Run Code Online (Sandbox Code Playgroud)
这给了这个:
> z
testzoo01.csv testzoo02.csv
1951-01-01 22.9 22.9
1951-01-02 4.3 4.3
1951-01-03 4.6 4.6
Run Code Online (Sandbox Code Playgroud)
如果需要,可以通过在Filenames变量上放置名称来修改名称,例如names(Filenames) <- gsub("testzoo|.csv", "", Filenames),或者通过修改结果的名称,例如,names(z) <- gsub("testzoo|.csv", "", names(z))
(2)来自动物园对象.如果他们之前已经阅读过,那么试试这个:
# create test objects using Lines and library() statement from above
testobj1 <- testobj2 <- read.zoo(textConnection(Lines), header = TRUE, sep = ",")
# merge them into a single zoo object
zz <- do.call(merge, sapply(ls(pattern = "testobj.*"), get, simplify = FALSE))
Run Code Online (Sandbox Code Playgroud)
这给了这个:
> zz
testobj1 testobj2
1951-01-01 22.9 22.9
1951-01-02 4.3 4.3
1951-01-03 4.6 4.6
Run Code Online (Sandbox Code Playgroud)
如上所述,zz可以进一步修改名称.