[R] rename files in R

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Sat Sep 17 03:29:31 CEST 2022

The syntax might not be quite right, but why not something like file.rename(colnames(dataframe)[1]) -- Using colnames to get the names of the columns that are in the first row, selecting the first element from colnames() and setting the file name equal to that.

Do a for loop using current file names in some folder, and save to a new folder.


-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Kai Yang via R-help
Sent: Friday, September 16, 2022 1:52 PM
To: R-help Mailing List <r-help using r-project.org>; Rui Barradas <ruipbarradas using sapo.pt>
Subject: Re: [R] rename files in R

[External Email]

 Hello,Here is the example:
  file name    first row  file1.txt     abc.txt  file2.txt     bed.txt  file3.txt     gogo.txt  . . file1243.txt    last.txt
I want to use loop because I need to read the first row information for first file, and then rename the file, then go to next file. I'm not sure if this is right way to approach my goal. Any suggestion will be appreciated. Thanks, Kai    On Friday, September 16, 2022 at 10:38:32 AM PDT, Rui Barradas <ruipbarradas using sapo.pt> wrote:


Please post the first row of 2 or 3 files and the expected result.

You can rename files with ?file.rename. This function is vectorized its on arguments so you do not need a loop, only the source and destination filenames. Both vectors should have the same length, if not strange things will occur including data loss.

Hope this helps,

Rui Barradas

Às 18:26 de 16/09/2022, Kai Yang via R-help escreveu:
> Hello,I have a lot of files with not meaningful name, such as:  
> file1.txt, file2.txt ...... I need to rename them using the 
> information from the first row of the files. Now I can get the 
> information from the first row of each file. Now, I need know how to 
> rename them in R (using loop?). Thank you for your helpKai
>     [[alternative HTML version deleted]]
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl
> .edu%7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C637989475519163868%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> &sdata=lPW2XrQntw3V7eJmxkaoOXK75%2FO1hWgRv7FO3%2B5O3OQ%3D&rese
> rved=0 PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%
> 7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C637989475519320101%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwM
> DAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&
> sdata=%2FaSxvb47qFp7Mo6MFlCEQcvinHvV%2FWA%2Brs3keoNuTqk%3D&reserve
> d=0 and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7C559ca95ca7d34937799a08da980c3995%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C637989475519320101%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=%2FaSxvb47qFp7Mo6MFlCEQcvinHvV%2FWA%2Brs3keoNuTqk%3D&reserved=0
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list