title: Winsorizing Panel Data (dataframe) by year and Trimming for certain conditions
body: <p>I'm trying to winsorize (replacing outliers with values that are 2 or 3 standard deviations away from the mean) on panel data and for each year. I installed the robustHD package which includes such a function, however I'm unable to implement it on my dataset.</p>
  <p>My dataset looks similar to Grunfeld (although I have NA's) with different firms over the same years. (1935-1954)</p>
  <pre><code>&gt; head(Grunfeld, 6)
    firm year   inv  value capital
  1    1 1935 317.6 3078.5     2.8
  2    1 1936 391.8 4661.7    52.6
  3    1 1937 410.6 5387.1   156.9
  4    1 1938 257.7 2792.2   209.2
  5    1 1939 330.8 4313.2   203.4
  6    1 1940 461.2 4643.9   207.2
  </code></pre>
  <p>What i would like to do now is to winsorize only certain columns (i.e. inv and value) over all firms and store it in a dataframe with the same structure. I tried the following code:</p>
  <h1>The goal is basically to obtain a winsorized copy of the original dataframe (everything structured in the same way).</h1>
  <p>As proposed below, plyr is a good way to split dataframes and apply functions but i'm unable to get it to work.</p>
  <pre><code>library(plm)
  library(robustHD)
  library(plyr)
  
  data(&quot;Grunfeld&quot;, package=&quot;plm&quot;)
  
  #Winsorize data  each year (over all firms) therefore split dataframe for each year and apply the winsorize function)
  
  Grunfeld.w&lt;-ddply(Grunfeld, .(year) function(x) winsorize(x$inv,x$value))
  </code></pre>