0

I am working with data.frames that have very similar names (df1, df2,.. df7). Because most of the analysis will be done in all, I would like to automatize the process as much as possible. I have already managed to use assign when reading the files:

for(i in 1:7) { 
  NameFile <- paste("df",i,"_10/score.out", sep="")
  OutFile <- read.table(NameFile, na.string="NA",header=TRUE,sep="\t")
  NameVar <- paste("df",i,"_10", sep="")
  assign(NameVar, OutFile)
  }

But now I would also like to perform the operation/function also in all of them. For instance subsetting:

for(i in 1:7) {
              Newname <- paste("dfsmall",i,sep="")
              dftemp <- subset(df[i], p == 0   & abs(sepscore) > 0.3)
              assign(Newname, dftemp)
              }

I think most of the is correct but how do I call df[i]? That is, the objects that already exist with the names df1, df2(..)? And is there a cleaner way to do this?

There are a few similarish questions here, but none that would find suitable for my problem. Then again, I am prone to missing the obvious.

Thanks.

fridaymeetssunday
  • 1,118
  • 1
  • 21
  • 31
  • This may be of use to you: http://stackoverflow.com/questions/5158830/identify-all-objects-of-given-class-for-further-processing – Chase Jul 03 '12 at 14:23
  • Interesting chase. In particular, I was not aware that one could use lapply to generate plots from a list of objects (lapply(outList, plot)). Then again, I am not yet comfortable with lapply. – fridaymeetssunday Jul 03 '12 at 15:07

2 Answers2

5

By using function get (work the same as assign but in reverse):

 for(i in 1:7) {
          get(paste("df",i,"_10",sep="")) -> df
          Newname <- paste("dfsmall",i,sep="")
          dftemp <- subset(df, p == 0   & abs(sepscore) > 0.3)
          assign(Newname, dftemp)
          }
plannapus
  • 18,529
  • 4
  • 72
  • 94
3

Let's say you have an analysis that for some reason has 10 components, I'd not call them df1 to df10, but order in a list:

data_list = list(exp1 = data.frame(...), 
                 exp2 = data.frame(...), 
                 ..., 
                 exp10 = data.frame(...))

Performing an operation for all experiments now involves writing a function which performs the operation, given the data for an element:

do_stuff = function(experiment_data) {
   # do something
 }

and use an apply style loop, in this case lapply:

result = lapply(data_list, do_stuff)

I think this approach is much easier.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • A tad too complex to me as I don't particularly like or know lists. I understand their advantages but I am not quite there yet. Thanks you and I'll keep this in mind. – fridaymeetssunday Jul 03 '12 at 15:10
  • 3
    @krespim, learn lists, learn lapply. In the long run (and even the medium and short runs) it will make your life easier and avoid problems. Look at Paul's answer, the key part is 1 line and that line is a lot less dangerous than using assign. – Greg Snow Jul 03 '12 at 17:03
  • I agree, the solution with assign is harder to read imo. Using assign and get is less transparent and easier to get wrong. – Paul Hiemstra Jul 03 '12 at 18:12