使用文件名命名列

发布于 2024-12-02 06:31:21 字数 241 浏览 1 评论 0原文

我有数百个 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”。有没有一种自动的方法来做到这一点?

谢谢

I have hundreds of csv files (zoo objects in R) with 2 columns:

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

I want the second column to have the name of each file. For example, when a filename is 02O_zoo.csv I would like the second column to be "02O" instead of "pp". Is there an automatic way of doing this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-12-09 06:31:21

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

(1) From files read.zoo can take a character vector of file names as its first argument so:

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

which gives this:

> 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

It would be possible to modify the names further if desired by placing names on the Filenames variable, e.g. names(Filenames) <- gsub("testzoo|.csv", "", Filenames), or by modifying the names of the result, e.g. names(z) <- gsub("testzoo|.csv", "", names(z))

(2) From zoo Objects. If they have been read in previously then try this:

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

which gives this:

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

The names of zz could be modified further as in the discussion above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文