title: Creating empty data frame of varying length to store results from for loop
body: <p>I have three variables in my dataset, as State, Year and Serotype. The code I have below is to aggregate the line listed data. I have created empty data frames to store results of for loop for Agg.Res 1 2..and so on. My problem is How do I go about making empty data frames to store results for different years? I want to make calculations for each year. When I run this code it just does the calculation for 2013 because I haven't created empty data frame, for data7, to store results for each year. Any help would be much appreciated.  </p>
  
  <pre><code>Agg.Res &lt;- data.frame(matrix(NA, nrow=11, ncol=17)) 
  for( i in 1:50 ){ # I am creating 50 sequentially numbered data frames
  dataframe.name &lt;- paste( "Agg.Res",i, sep="") # Names the matrix
  assign( dataframe.name, Agg.Res, envir = .GlobalEnv) # Assigns template dataframe to name
  }
  
  #For State Illinois
  data6&lt;-data3[which(data3$State=="Illinois"),]
  
  for(i in 2003:2013){  # loop for different years
  data7&lt;-data6[which(data6$YEAR==i),]
  
  Ent1&lt;-data7[which(data7$SEROTYPE_GR=="A"),]
  Agg.Res1[i-2002,]&lt;-colSums(Ent1[,31:47], na.rm=T)/nrow(Ent1)
  
  Ent2&lt;-data7[which(data7$SEROTYPE_GR=="B"),]
  Agg.Res2[i-2002,]&lt;-colSums(Ent2[,31:47], na.rm=T)/nrow(Ent2)
  
  Ent3&lt;-data7[which(data7$SEROTYPE_GR=="C"),]
  Agg.Res3[i-2002,]&lt;-colSums(Ent3[,31:47], na.rm=T)/nrow(Ent3)
  
  Ent4&lt;-data7[which(data7$SEROTYPE_GR=="D"),]
  Agg.Res4[i-2002,]&lt;-colSums(Ent4[,31:47], na.rm=T)/nrow(Ent4)
  
  Ent5&lt;-data7[which(data7$SEROTYPE_GR=="E"),]
  Agg.Res5[i-2002,]&lt;-colSums(Ent5[,31:47], na.rm=T)/nrow(Ent5)
  }
  </code></pre>
  
  <p>The data looks like this:</p>
  
  <pre><code>State      Year          Serotype    Drug A    Drug B     Drug C . . . . 
  
  Illinois   2003            A          1          0          1    . .. .  
  
  Illinois   2003            B          0          0          1    . . . . 
  
   .          .              .           .          .          .    . . . 
   .          .              .           .          .          .    . . .
  Missouri   2008            E           1          1          1  . . . . 
  </code></pre>
  
  <p>The year ranges from 2003:2013; Serotype ranges from A:E; also includes various states. If a serotype is resistant to a drug its given by 1, if its not resistant then its 0; Binary variables. </p>