1

I am trying to do something that may not be possible in R, but I'm largely R self-taught so there is a good chance that I'm making an obvious error.

I am trying to use the list.files function to list only the files in a folder that are also listed in a reference data frame.

My data frame looks like this:

> G2_1A27
     Treatment             Date Chamber_ID GasmetID       Spectrum.file
24        1A27  8/28/2018 11:51       <NA>       G2 Spectrum_000024.spe
25        1A27  8/28/2018 11:53       <NA>       G2 Spectrum_000025.spe
26        1A27  8/28/2018 11:55       <NA>       G2 Spectrum_000026.spe
27        1A27  8/28/2018 11:57       <NA>       G2 Spectrum_000027.spe
28        1A27  8/28/2018 11:59       <NA>       G2 Spectrum_000028.spe
29        1A27  8/28/2018 12:01       <NA>       G2 Spectrum_000029.spe
30        1A27  8/28/2018 12:03       <NA>       G2 Spectrum_000030.spe
31        1A27  8/28/2018 12:05       <NA>       G2 Spectrum_000031.spe

The list of files I want are the spectrum files under the Spectrum.file column and the folder I'm listing from contains the actual files and then some from other treatments. I have separate dataframes for each treatment and the spectrum files are split up by date.

current.folder <- "G:\\Team Drives\\USDA_SCRI\\UCSC_field_trial_results\\Gasmet\\G2\\180828_180829_G2\\Samples\\"
setwd("G:\\Team Drives\\USDA_SCRI\\UCSC_field_trial_results\\Gasmet\\G2\\180828_180829_G2\\Samples\\")
new.folder <- "C:\\Users\\pres9340\\Desktop\\test"
list.of.files = list.files(current.folder, G2_1A27$Spectrum.file)

I didn't expect this to work but when it runs it does reference the data frame but only outputs the first file rather than listing all files in the folder that match the files in the dataframe.

Kaliber
  • 23
  • 1
  • 5
  • Cant debug at the minute but something like : `list.of.files <- list.files(current.folder)` then `list.of.files[list.of.files %in% G2_1A27$Spectrum.file]` – Ian Wesley Dec 04 '18 at 21:59
  • 1
    Another option could be `pattern <- paste(G2_1A27$Spectrum.file, collapse = '|'); list.files(pattern = pattern)`. – Rui Barradas Dec 04 '18 at 22:05
  • @IanWesley debug or no that worked beautifully thank you – Kaliber Dec 04 '18 at 22:48

1 Answers1

0

Depends on what you intend to do with matching list of files, something like this:

# get full names including folder path
list.of.files = list.files(current.folder, full.names = TRUE)

# then keep only the basename (file names) matching dataframe column
clean.list.of.files <- list.of.files[ basename(list.of.files) %in% G2_1A27$Spectrum.file ]

# then read the data
myData <- lapply(clean.list.of.files, read.table)

Edit: if we want to copy:

file.copy(clean.list.of.files, new.folder)
zx8754
  • 52,746
  • 12
  • 114
  • 209
  • The next step after this is to use file.copy to copy the files into a new folder. – Kaliber Dec 04 '18 at 22:29
  • I ran your code any it works until reading the data then it returns an error: Error in scan: ...line 2 did not have 3 elements – Kaliber Dec 04 '18 at 22:39
  • @Kaliber I added copy option. Regarding error in scan, please provide [example reproducible data](https://stackoverflow.com/q/5963269/680068), so we can test our codes. – zx8754 Dec 05 '18 at 07:01