0

In R, I have a list of files with characters, how do I know which file in the list contains a description, for example: "Test Word".

[[1]]
[1] "V:/Planej/2021/xv/02 Fev/x/Test Word"

[[2]]
[1] "V:/Planej/2021/xv/02 Fev/y/Test X"

[[3]]
[1] "V:/Planej/2021/xv/02 Fev/z/XXX"

[[4]]
[1] "V:/Planej/2021/xv/02 Fev/q/Plan"

There will only be one file that contains the word "Test World", and all of these files are "xlsx".

Theorp
  • 151
  • 8
  • 1
    By opeing each file, reading its contents and then searching the contents for the phase you are interested in. Is the phrase guaranteed to be in at least one file? In exactly one file? Possibly in one or more files? If the files might contain more than one line, does it matter if the phrase wraps over lines? In what format are your files? If the file format defines rows and cells (eg xlsx or csv) should the search be limited to a subset of rows and/or columns. Please provide test data and indicate what you have tried so far. – Limey Feb 18 '21 at 13:13
  • thanks, I filled it out. – Theorp Feb 18 '21 at 13:17
  • 1
    If your files are all `xlsx`, is there a reason they are all lacking that file extension? Have you researched how to read in one file, such as using `readxl` or `openxlsx`? Once you can check *one file* for the presence of your pattern, iterate that over all files using `sapply` or similar. – r2evans Feb 18 '21 at 13:40
  • 1
    While not a perfect match, https://stackoverflow.com/a/24376207/3358227 discusses how (and why) to process a list-of-frames instead of individual frames in an R environment. This applies here because they discuss reading in multiple files in one step, often using `lapply(filelist, read.csv)` or similar. To adapt to your situation, replace `read.csv` with whatever function you are using to read in an xlsx file. Good luck. – r2evans Feb 18 '21 at 13:42

0 Answers0