使用文件名命名列
我有数百个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(1) 来自文件
read.zoo
可以将文件名的字符向量作为其第一个参数,因此:这给出了:
如果通过将名称放在
Filenames
变量上,例如names(Filenames) <- gsub("testzoo|.csv", "", Filenames)
,或者修改结果的名称,例如names(z) <- gsub("testzoo|.csv", "", names(z))
(2) 来自动物园对象。如果之前已读过它们,请尝试以下操作:
给出以下结果:
zz
的名称可以按照上面的讨论进一步修改。(1) From files
read.zoo
can take a character vector of file names as its first argument so:which gives this:
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:
which gives this:
The names of
zz
could be modified further as in the discussion above.