Can I put the result of read.files in a list and iterate through the list to match specific files

all_files = NULL
names_files<- list.files(path, pattern = ".csv")
for(i in 1:length(names_files)){
  for(j in 1:length(first_path)){
    if(names_files[i] == first_path[j]{
      renamefiles()
    })
  }
}

It is not entirely clear what you are doing given that we don't know what first_path looks like and we don't know what the function renamefiles() does.

I will say that when you use list.files it returns a vector that can be iterated over just like any other vector or list.

In addition, there is already a base function for renaming files (file.rename) if you weren't aware.


Also, it looks like your code was not formatted correctly to make it easy to read for people trying to help you. Formatting code allows for people to more easily identify where issues may be occurring, and makes it easier to read, in general. I have edited you post to format the code properly.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (`mutate`) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

```
example <- foo %>%
  filter(a == 1)
```

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code

Thank you for your response. Please see program below. What I want to do is to list the files in the directory that match the ".csv" extension and then iterate over the list of files and over the first_path list, and if the file name in the list of all.files[i] is identical to the file name in the list first_path[j], then I would like to store those file names in another list, but when I try to print the output of if(all_files[i] == first_path[j]), not output is produced.


selected_files = NULL
list1 <- list("JF_160426_Dep2Plas_tryp_Gpep_SIDtargPSMs.csv", "JF_160426_Dep2Plas_ctryp_Gpep_SIDtargFULLPSMs.csv",
              "A160824_JF_udep_tryp_Hi_SIDdda_FULL_NewParsePSMs.csv", "A150413_JF_GPeps_SIDtarg_GPstdMix_Tryp_2runs_v3PSMs.csv")

for(i in 1:length(list1)){
  first_path[i] <- paste(getwd(), "/", list1[i], sep = "")
}
#> Error in eval(expr, envir, enclos): object 'first_path' not found
  
  all_files <- list.files(path, pattern = ".csv")
#> Error in list.files(path, pattern = ".csv"): object 'path' not found
  for(i in 1:length(all_files)){
    print(all_files[i])
  }
#> Error in eval(expr, envir, enclos): object 'all_files' not found
  
  selected_files = NULL
   for(i in 1:length(all_files)){
     for(j in 1:length(first_path)){
       if(all_files[i] == first_path[j]){
          selected_files[j] <-  first_path[j]
          print(selected_files[j])
     }
   }
 }
#> Error in eval(expr, envir, enclos): object 'all_files' not found

Created on 2018-09-17 by the reprex package (v0.2.0).

So there are a couple things to address here:

  1. Your first and second for loops are unnecessary. paste and print are vectorized functions so as long as the inputs are all the same length or of length 1 then you don't need to use this function in a loop, also, you can just use sep = "/":

    first_path <- paste(getwd(), list1, sep = "/")
    all_files <- list.files(path, pattern = ".csv")
    print(all_files)
    
  2. You don't really need the for loop for the last loop either. You can just use vector subsetting methods to only include the file names in your first_path dataset that are also in your all_files dataset

    selected_files <- first_path[first_path %in% all_files]
    selected_files
    

While you can certainly accomplish what you are looking for with for loops, they are likely to slow down your code (maybe not here if all_files is not large) but it is useful to get in the habit of using R's build in vectorized functions.

2 Likes

Thank you very much. I appreciate it.