1

I just want to use use 10 files in R. For each I want to calculate something. Exp. file: stat1_pwg1.out stat23_pwg2.out .. stat45_pwg10.out

I try this:

for (i in 1:10){
Data=paste("../XYZ/*_pwg",i,".out",sep="")
line=read.table(Data,head=T)
}

But it does not work? Any hinds?

Jasmine
  • 149
  • 3
  • 7
  • 3
    Quite similar to http://stackoverflow.com/q/5758084/602276 and http://stackoverflow.com/q/3764292/602276 – Andrie Jul 20 '11 at 09:00

3 Answers3

4

I suspect your problem comes from the wildcard *. A better way to do this might be to first store the file names using dir, then find the ones you want.

files <- dir("../XYZ",pattern="stat[0-9]+_pwg[0-9]+\.out")
for(f in files) {
  line=read.table(Data,head=T)
}

You could also use one of the apply family of functions to eliminate the for loop entirely.

Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235
  • You're welcome. Since you're new to SO I will offer the following suggestion: If this answered the question please click the little checkbox to mark it as answered. This will keep the question from being listed in the "unanswered" questions and helps keep the SO database tidy. – Ari B. Friedman Jul 20 '11 at 09:47
  • @gsk3: remember to escape the `.` in your regex. – Richie Cotton Jul 20 '11 at 10:14
2

A few things about your code.

paste is vectorised, so you can take it out of the loop.

paste("../XYZ/*_pwg", 1:10, ".out", sep = "")

(Though as you'll see in a moment, you don't actually need to use paste at all.)

read.table won't accept wildcards; it needs an exact match on the file name.

Rather than trying to construct a vector of the filenames, you might be better using dir to find the files that exist in your directory, filtered by a suitable naming scheme.

To filter the files, you use a regular expression in the pattern argument. You can convert from wildcards to regular expression using glob2rx.

file_names <- dir("../XYZ", pattern = glob2rx("stat*_pwg*.out"))
data_list <- lapply(filenames, read.table, header = TRUE)

For a slightly more specific fit, where the wildcard only matches numbers than anything, you need to use regular expressions directly.

file_names <- dir("../XYZ", pattern = "^stat[[:digit:]]+_pwg[[:digit:]]+\\.out$")
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
0
    files <- dir(pattern="*Rip1_*")

    files

    for (F in files){ assign(F , Readfunc(F))}
  • 3
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Mar 27 '17 at 10:20