使用文件名命名列

时间:2011-09-01 14:59:31

标签: r filenames zoo

我有数百个csv文件(R中的zoo对象),有2列:

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

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

由于

1 个答案:

答案 0 :(得分:8)

(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)

给出了这个:

> 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的名称可以在上面的讨论中进一步修改。