7

我有数百个包含 2 列的 csv 文件(R 中的动物园对象):

"Index","pp"
1951-01-01,22.9
1951-01-02,4.3
1951-01-03,4.6

我希望第二列包含每个文件的名称。例如,当文件名是02O_zoo.csv我希望第二列是“02O”而不是“pp”。有自动的方法吗?

谢谢

4

1 回答 1

8

(1) From files 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)

这给出了这个:

> 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

如果需要,可以通过在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))

这给出了这个:

> zz
           testobj1 testobj2
1951-01-01     22.9     22.9
1951-01-02      4.3      4.3
1951-01-03      4.6      4.6

的名称zz可以如上面讨论的那样进一步修改。

于 2011-09-01T15:10:18.120 回答