简体   繁体   English

如何重命名以“.csv”结尾的文件夹中的文件??[R]

[英]How to rename files in a folder with ".csv" at the end??[R]

setwd("C:\\Users\\Note\\Documents\\Folder")
n <- dir(pattern = ".csv")
names<-as.character(c(1:length(n)))
file.rename(n,names)

I am trying to rename several worksheets to an id 1,2,3,4,5,6 etc But when I do this the worksheets are no longer ".csv" files.我正在尝试将几个工作表重命名为 ID 1、2、3、4、5、6 等但是当我这样做时,工作表不再是“.csv”文件。 How to add ".csv" to the rename function?如何将“.csv”添加到重命名功能? Is there any way to make "n" stay in sequence 1,2,3,4,5,6, so that if I add a new spreadsheet it will be the last one in "n"?有没有办法让“n”保持在1、2、3、4、5、6的顺序,这样如果我添加一个新的电子表格,它将是“n”中的最后一个? n = https://imgur.com/Z1KVqh2 n = https://imgur.com/Z1KVqh2

Try this instead of your third line试试这个而不是你的第三行

names <- paste0(1:length(n), ".csv")

The numbers will be automatically coerced to character format.数字将被自动强制转换为字符格式。

另一个选项是seq_along ,即使对于零长度 'n' 也有帮助

names <- paste0(seq_along(n), ".csv")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM