Home > OS >  Files disappeared after being renamed using file.rename
Files disappeared after being renamed using file.rename

Time:08-28

I tried to use R to rename csv files in a folder with a loop. One of the parameters used in the process was the unique value of a column of a csv file after being imported as a data frame, which I misspecified (I misspelled "Reporter.ISO" as "ReporterISO"). After running the loop, there are only some of the files left in the folder. I want to know where the disappeared files went.

setwd([some folder])
versions= c("1992", "1996", "2002", "2007", "2012", "2017", "2022")
names(versions) = c("H0", "H1", "H2", "H3", "H4", "H5", "H6")
files = list.files()

for (i in files){
  df = read.csv(i)
  reporter = tolower(unique(df$Reporter.ISO))
  v = versions[unique(df$Classification)]
  to = paste0("imports_2007_", reporter, "_hs6_v", v, ".csv")
  file.rename(i, to)
}

Are there any history of what R did that I could find or something similar?

Any help is appreciated.

CodePudding user response:

Your code is actually nice and works well. The problem is that there might be an unexpected error and there is no undo option. I probably would use a function that by default doesn't execute the renaming but throws the new file name instead. So you are able to check thoroughly before you arm the function.

fun <- function(x, run=FALSE) {
  df <- read.csv(x)
  reporter <- tolower(unique(df$Reporter.ISO))
  v <- versions[unique(df$Classification)]
  to <- paste0("imports_2007_", reporter, "_hs6_v", v, ".csv")
  if (run) {
    file.rename(x, to)
  } else {
    return(to)
  }
}

## setup
setwd('~/testdir')
versions <- c(H0="1992", H1="1996", H2="2002", H3="2007", H4="2012", H5="2017", H6="2022")
files <- list.files()

Looping the function over the file names in an sapply gives a vector of the new file names named by the old file names. It doesn't execute the renaming by default. Now run some tests until you're convinced it will be right.

test <- sapply(files, fun)
test
# file1.csv                      file2.csv 
# "imports_2007_1_hs6_v1992.csv" "imports_2007_2_hs6_v1996.csv" 
# file3.csv                      file4.csv 
# "imports_2007_3_hs6_v2002.csv" "imports_2007_4_hs6_v2007.csv" 
# file5.csv                      file6.csv 
# "imports_2007_5_hs6_v2012.csv" "imports_2007_6_hs6_v2017.csv" 
# file7.csv 
# "imports_2007_7_hs6_v2022.csv" 

any(duplicated(test))  ## example, test if there are duplicates
# [1] FALSE

list.files()  ## see file names are not changed
# [1] "file1.csv" "file2.csv" "file3.csv" "file4.csv" "file5.csv"
# [6] "file6.csv" "file7.csv"

If you set run=TRUE you arm the function and renaming is executed.

sapply(files, fun, run=TRUE)

list.files()  ## now they are changed
# [1] "imports_2007_1_hs6_v1992.csv" "imports_2007_2_hs6_v1996.csv"
# [3] "imports_2007_3_hs6_v2002.csv" "imports_2007_4_hs6_v2007.csv"
# [5] "imports_2007_5_hs6_v2012.csv" "imports_2007_6_hs6_v2017.csv"
# [7] "imports_2007_7_hs6_v2022.csv"

Data:

dat <- list(file1 = structure(list(X = 1:3, x = 1:3, Classification = c("H0", 
"H0", "H0"), Reporter.ISO = c(1L, 1L, 1L)), class = "data.frame", row.names = c(NA, 
-3L)), file2 = structure(list(X = 1:3, x = 1:3, Classification = c("H1", 
"H1", "H1"), Reporter.ISO = c(2L, 2L, 2L)), class = "data.frame", row.names = c(NA, 
-3L)), file3 = structure(list(X = 1:3, x = 1:3, Classification = c("H2", 
"H2", "H2"), Reporter.ISO = c(3L, 3L, 3L)), class = "data.frame", row.names = c(NA, 
-3L)), file4 = structure(list(X = 1:3, x = 1:3, Classification = c("H3", 
"H3", "H3"), Reporter.ISO = c(4L, 4L, 4L)), class = "data.frame", row.names = c(NA, 
-3L)), file5 = structure(list(X = 1:3, x = 1:3, Classification = c("H4", 
"H4", "H4"), Reporter.ISO = c(5L, 5L, 5L)), class = "data.frame", row.names = c(NA, 
-3L)), file6 = structure(list(X = 1:3, x = 1:3, Classification = c("H5", 
"H5", "H5"), Reporter.ISO = c(6L, 6L, 6L)), class = "data.frame", row.names = c(NA, 
-3L)), file7 = structure(list(X = 1:3, x = 1:3, Classification = c("H6", 
"H6", "H6"), Reporter.ISO = c(7L, 7L, 7L)), class = "data.frame", row.names = c(NA, 
-3L)))

setwd('~/testdir')
lapply(names(dat), \(x) write.csv(dat[[x]], paste0(x, '.csv')))
  •  Tags:  
  • r
  • Related