[R] How to rename file names with condition
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Tue Jul 23 17:21:32 CEST 2019
Assuming you have the vector of .xlsx file names as oldFilenames
then
newname <- function(oldname) {
yr <- substr(oldname,7,10)
m <- as.numeric(substr(oldname,11,12))
mo <- as.integer( m/3 + 1 - 0.1 )
i <- c(3,1,2)[ m%%3 + 1 ]
sprintf("Domain%s-%d-%d.txt",yr,mo,i)
}
for ( s in oldFilenames )
file.rename(s, newname(s) ) }
HTH,
Eric
On Tue, Jul 23, 2019 at 6:01 PM danielmessay--- via R-help <
r-help using r-project.org> wrote:
> Could somebody please help me on this?
>
> I have many files on my computer and would like to change its names so
> that it would be more meaningful for me. The filenames are in the following
> format: cvcvcv198307.xlsx
>
> I want to change it in such a way that
> cvcvcv into Domain1983 want to keep it 07 into 3-1 * And here is my main
> problem (Explanation below).
>
>
> Each month (01-12) is divided into three data points. Meaning,
>
> 01(Jan) has three data points: 1, 2, and 3
> 02(Feb) has the next three data points: 4,5, and 6...12(Dec) will have:
> 34,35, and 36
>
>
> So, 01 (last digit in my file name) means, first month, first data point,
> i.e. 1-1
>
> 02 - First month, and second data point, i.e. 1-2
>
> 03 - First month, third data point, i.e 1-3
>
> 04- second month, first data point, i.e. 2-1
>
> 05- 2-2
>
> 06- 2-3
>
> 07 - 3-1
>
> 08 - 3-2
>
> 09 - 3-3
>
> 10 - 4-1
>
> 11 - 4-2
>
> 12 - 4-3
>
> .
>
> .
>
> .
> 35 - 12-2
> 36 - 12-3
>
> And that's all.
>
> I have tried file.rename along with sapply for my whole sets of data
>
> (1:18,000). But i can only manage to change the file names before the last
> two #digits.
>
> Expected result.
>
> The whole file names changed from
>
>
> cvcvcv198307.xlsx into Domain1983-3-1.txtcvcvcv198414.xlsx
> into Domain1984-5-2.txt
>
>
>
>
> (Yes, file name extension too!)
>
> Thank you guys and I really appreciate any help on this.
> Daniel
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list