title: Filter by subcategory and running function in r
body: <p>I have data like this: </p>
  
  <pre><code>  Ind_name Co_Name Year EPS BPS
  1        A       X 2001  10  11
  2        A       X 2002  12  12
  3        A       X 2003  15  15
  4        A       Y 2001  10  18
  5        A       Y 2002  16  25
  6        A       Y 2003  20  26
  7        B       Z 2001  14  36
  8        B       Z 2002  16   9
  9        B       Z 2003  20  10
  </code></pre>
  
  <p>Similarly, in my dataset there are 50 industry, and each industry there are 15 companies and for each company there is data for 20 years. </p>
  
  <p>I want following things : 
  1. Filter data of all companies for particular year of a specific industry. Eg. </p>
  
  <pre><code>    Ind_name Co_Name Year EPS BPS
  1        A       X 2001  10  11
  4        A       Y 2001  10  18
  </code></pre>
  
  <p>After that i need to calculate correlation coefficient, and regression (slope) beta between EPS and BPS.   </p>
  
  <p>I want output like this :</p>
  
  <pre><code>Ind_name   Year   Correlation    Beta
   A          2001    .55 #assume   1.2 #assume
   A          2002    .85 #assume   0.2 #assume 
   A          2003    .65 #assume   .65 #assume
  </code></pre>
  
  <p>Can you please suggest me how this can be achieved ? </p>
  
  <p>Note : correlation and regression slope can be calculated as follows:</p>
  
  <pre><code>cor(EPS,BPS)  #corrleation
  summary(lm(mydata$EPS~mydata$BPS))$coefficients[2] #directly give beta
  </code></pre>