(1) Da fileread.zoo
può prendere un vettore carattere di nomi di file come il suo primo argomento così:
# 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)
che conferisce a questo:
> 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
E ' sarebbe possibile modificare ulteriormente i nomi, se desiderato, inserendo nomi sulla variabile Filenames
, ad es. names(Filenames) <- gsub("testzoo|.csv", "", Filenames)
o modificando i nomi del risultato, ad es. names(z) <- gsub("testzoo|.csv", "", names(z))
(2) Da zoo Oggetti. Se essi sono stati letti in precedenza quindi provare questo:
# 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))
che conferisce a questo:
> zz
testobj1 testobj2
1951-01-01 22.9 22.9
1951-01-02 4.3 4.3
1951-01-03 4.6 4.6
I nomi di zz
potrebbero essere modificate ulteriormente come nella discussione di cui sopra.
grazie, questo è un modo molto pulito di farlo! – sbg