Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

r - Get name of file in read.csv same as in write.csv

I would like to achieve that the file I write.csv has the same name as the file I use with read.csv2. See below: I am reading a file name "2005Calc.csv" and want to write it as: "2005Calc28-01-2021.csv". How can I do this? I tried something below and it works..only I am not able to add the csv file name to it.

Thank you in advance.

    LKCalc <- read.csv2("2005Calc.csv")

    date<-format(Sys.time(), "%d-%m-%Y")

    csvFileName <- paste("FILENAME",date,".csv",sep="")

    write.csv(LKCalc, file=csvFileName) 
question from:https://stackoverflow.com/questions/65939776/get-name-of-file-in-read-csv-same-as-in-write-csv

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

"FILENAME" is a literal string, not a variable referring to a file name. How about (untested):

FILENAME <- "2005Calc"
LKCalc <- read.csv2(paste0(FILENAME,".csv"))
date <- format(Sys.time(), "%d-%m-%Y")
csvFileName <- paste0(FILENAME,date,".csv")
write.csv(LKCalc, file=csvFileName) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...