0

I need to create a bunch of subset data frames out of a single big df, based on a date column (e.g. - "Aug 2015" in month-Year format). It should be something similar to the subset function, except that the count of subset dfs to be formed should change dynamically depending upon the available values on date column

All the subsets data frames need to have similar structure, such that the date column value will be one and same for each and every subset df.

Suppose, If my big df currently has last 10 months of data, I need 10 subset data frames now, and 11 dfs if i run the same command next month (with 11 months of base data).

I have tried something like below. but after each iteration, the subset subdf_i is getting overwritten. Thus, I am getting only one subset df atlast, which is having the last value of month column in it.

I thought that would be created as 45 subset dfs like subdf_1, subdf_2,... and subdf_45 for all the 45 unique values of month column correspondingly.

uniqmnth <- unique(df$mnth)

for (i in 1:length(uniqmnth)){

    subdf_i <- subset(df, mnth == uniqmnth[i])

    i==i+1

}

I hope there should be some option in the subset function or any looping might do. I am a beginner in R, not sure how to arrive at this.

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Bala
  • 193
  • 1
  • 9
  • What have you already tried? Does `split()` cover your needs? Otherwise, look into the dplyr package. – Heroka Aug 31 '15 at 07:24
  • 1
    @Bala Please [read](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to know how to ask a reproducible example. – agstudy Aug 31 '15 at 07:44
  • 1
    The fact that you are using RStudio has nothing to do here. If you seeking for help, you should first make an effort and read the site policy regarding how to ask a question as already pointed above. – David Arenburg Aug 31 '15 at 07:48
  • @agstudy Thanks for the suggestions about reproducible example. I will keep in mind, going forward. – Bala Aug 31 '15 at 08:02
  • 1
    If you are using `for` loop, you may need `assign` to create new object names in the global environment. – akrun Aug 31 '15 at 08:05

1 Answers1

0

I think the perfect solution for this might be use of assign() for the iterating variable i, to get appended in the names of each of the 45 subsets. Thanks for the note from my friend. Here is the solution to avoid the subset data frame being overwritten each run of the loop.

uniqmnth <- unique(df$mnth)

for (i in 1:length(uniqmnth)){
  assign(paste("subdf_",i,sep=""), subset(df, mnth == uniqmnth[i])) i==i+1 
}
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Bala
  • 193
  • 1
  • 9