0

I am a fisheries stock assessment scientist and am currently struggling with this issue.

Basically, I want to create a new dataframe with a subset of the data from each fish stock that I am looking at. Ideally, I would like to do this with a for loop (or if there is a function that does this built into r or a package that would also be great).

The pseudo-code for this issue would be

FOR EACH fishStock IN dataFrame
    CREATE NEW DATAFRAME CALLED fishStock
    DATAFRAME(fishStock) = SUBSET OF dataFrame WHERE fishStock = fishStock

Once this process is completed, I should have a data frame for each fish stock listed in my original data frame that contains a subset of all the data for that stock.

Without a for loop, I would have to write out a line of code for each stock that would be long and look like this:

stock1<-subset(data,FishStock =="stock1")
stock2<-subset(data,FishStock =="stock2")
stock3<-subset(data,FishStock =="stock4")

Hopefully what I am trying to get across makes sense.

lmo
  • 37,904
  • 9
  • 56
  • 69
  • 3
    Use `split` to create a named list of data.frames. `myList <- split(data, data$FishStock)`. See gregor's answer to [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for tips on working with lists of data.frames. – lmo Mar 02 '17 at 12:12
  • Please, add some example data so we can reproduce your problem. Take a look at http://stackoverflow.com/a/5963610/709777 – pacomet Mar 02 '17 at 12:19

0 Answers0