简体   繁体   中英

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. How to add ".csv" to the rename function? 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 = 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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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