title: Trying to build vector of specific files within folders (R)
body: <p><strong>Edit:</strong> I think I solved this by using the getAbsolutePath function in the R.utils package.</p>
  
  <pre><code>wage_data_files &lt;- list.files("WageData", full.names=TRUE, recursive=TRUE)
  all_files &lt;- grep(paste(toMatch, collapse = "|"), wage_data_files, value=TRUE)
  files_vector &lt;- vector()
  for (i in seq_along(all_files){
  files_vector &lt;- c(files_vector, getAbsolutePath(all_files[i]))}
  </code></pre>
  
  <p>Thanks again for all the help. </p>
  
  <h3>#</h3>
  
  <p>I'm trying to extract a subset of .csv files within a collection of folders. I want to put them all into a vector, then extract a specific value from each file and place that value into a vector. The code my question is about regards how to get all the files I wish to extract values from into a vector, which I can then run a for loop over to extract my desired values and place them into vectors.</p>
  
  <p>This is the folder structure:</p>
  
  <p>Desktop -> WageData -> 21 folders (sic.1980.annual.by.area; sic.1981.annual.by area, up to sic.2000.annual.by.area) -> In each of the above folders are roughly 1000 .csv files. </p>
  
  <p>I'm trying to extract six of those .csv files in each folder: "Idaho -- Statewide", "Indiana -- Statewide", "Michigan -- Statewide", 
               "Oklahoma -- Statewide", "Texas -- Statewide" and  "Wisconsin -- Statewide" </p>
  
  <p>So there are a total of 126 files: 6 for each year, for 21 years. Here are a couple examples of what the specific files are named:</p>
  
  <p>sic.1980.annual 40000 (Oklahoma -- Statewide)</p>
  
  <p>sic.1980.annual 55000 (Wisconsin -- Statewide)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>setwd("~/Desktop")
  wage_data_files &lt;- list.files("WageData", full.names=TRUE)
  for (i in seq_along(wage_data_files)){
  year_files &lt;- list.files(wage_data_files[i])
  toMatch &lt;- c("Idaho -- Statewide", "Indiana -- Statewide", "Michigan --  Statewide", 
           "Oklahoma -- Statewide", "Texas -- Statewide", "Wisconsin -- Statewide")
  dat &lt;- data.frame()
  states_vector1 &lt;- c(dat, grep(paste(toMatch, collapse = "|"), year_files, value=TRUE))
  print(states_vector1)}
  </code></pre>
  
  <p>One problem I'm running into right away, when I try to debug, is that I can't get my results to print out correctly. When I put the curly brackets after the print statement, I get a list like this:</p>
  
  <p>[[1]]
  [1] "sic.1980.annual 16000 (Idaho -- Statewide).csv"</p>
  
  <p>[[2]]
  [1] "sic.1980.annual 18000 (Indiana -- Statewide).csv"</p>
  
  <p>[[3]]
  [1] "sic.1980.annual 26000 (Michigan -- Statewide).csv"</p>
  
  <p>[[4]]
  [1] "sic.1980.annual 40000 (Oklahoma -- Statewide).csv"</p>
  
  <p>[[5]]
  [1] "sic.1980.annual 48000 (Texas -- Statewide).csv"</p>
  
  <p>[[6]]
  [1] "sic.1980.annual 55000 (Wisconsin -- Statewide).csv"</p>
  
  <p>[[1]]
  [1] "sic.1981.annual 16000 (Idaho -- Statewide).csv"</p>
  
  <p>As you can see, it repeats after 6, even though wage_data_files is a vector of length 21. </p>
  
  <p>So my first problem is getting the desired files into a vector. My second problem is how to run a for loop reading those files in and then extracting my desired value. The issue I'm running into is how to set the working directory. Because, for the above function, the working directory is desktop. But to get the read.csv function to work, I'd have to set the working directory to each individual folder (e.g. "WageData/sic.1980.annual.by_area", "WageData/sic.1981.annual.by_area", etc...)</p>
  
  <p>Does anyone have any suggestions?</p>
  
  <p>Thank you. </p>