0

I'm trying to rename files in a WD folder from RStudio. The files are named with IDs and I want to replace the IDs with names. I have a reference file which is a dataframe(urban_o) with supplierID, companyname, and vendornumber. I tried this for loop but it doesn't seem to work. Error - the condition has length > 1 and only the first element will be used. Any ideas where I'm getting it wrong?

original_names <- list.files()

urba_o <- import("C:\Users\MaangiJ\Downloads\urba_o.xlsx")

# for loop
for (x in original_names){

  if(x == urba_o$supplierid[]){

    file.rename(x,urba_o$CompanyName[])
  }

}
josiah
  • 3
  • 1
  • Hi. Your attempt to select an element from a vector is simply not how R works. I would suggest looking through some of that. `x == urban_o$supplierid` (you don't need the `[]`) is a TRUE/FALSE vector comparing `x` against every element of `urban_o$supplierid`. So you are checking the `if()` condition against a logical vector, and therefore the error telling you that it is only looking at the first element. –  Sep 06 '22 at 12:57
  • Also `file.rename` is vectorized, so no for loop is needed. Something like `rows_to_rename = urba_o$supplierid %in% original_names` and then `with(urba_o[rows_to_rename, ], file.rename(supplierid, CompanyName))` should work, assuming everything was imported nicely. – Gregor Thomas Sep 06 '22 at 13:21
  • @GregorThomas Thank you for the solution. Nearly all my files were renamed except two throwing this error - cannot rename file '56' to '*Jkl Mmmk IN', reason 'The filename, directory name, or volume label syntax is incorrect' . Otherwise, my task is done. – josiah Sep 06 '22 at 14:37
  • Hello @Adam, thank you. I figured it should be `for (x in original_names) { for (z in urba_o$supplierid) { if (x == z){ } } }`. The code suggested by Gregor worked, just wondering how to pass file.name into the for loop here. Thank you. – josiah Sep 06 '22 at 14:44
  • Added an answer with a `for` loop option. A nested loop is certainly not needed here. – Gregor Thomas Sep 06 '22 at 14:51

1 Answers1

0

file.rename is vectorized, so no for loop is needed. Something like this should work:

## figure out which files are here and need renaming
rows_to_rename = urba_o$supplierid %in% original_names
## rename them
with(urba_o[rows_to_rename, ], file.rename(supplierid, CompanyName))

If you did want a for loop, this would work (though it will be less efficient, as well as longer to write):

for (i in 1:nrow(urba_o)) {
  if(urba_o$supplierid[i] %in% original_names) {
    file.rename(urba_o$supplierid[i], urba_o$CompanyName[i])
  }
}

Do note that you'll need to follow the file name rules for your operating system. For example, on Windows file names can't have the following reserved characters: <>/\*'":?|

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 1
    The two approaches work just fine. I had no Idea about the first one which I prefer now. And the reserved characters explain why I ran into the error for 2 suppliers. I have learned more than I thought by asking the question. Thank you – josiah Sep 06 '22 at 21:55