title: Dataframe manipulation with dplyr
body: <p>Here is a very small portion of my data frame, df :</p>
  
  <pre><code>     row.names   AB1         AB4         CB4852      CB4853      CB4854   CB4856         L1
  1   1_n         582.000       NA        375.00000   113.00000   160.0000 133.00000        1
  2   1_meanTOF   157.433       NA        44.62667    51.23009    42.5875  66.63158         1
  3   1_medianTOF 148.000       NA        38.00000    40.00000    31.5000  45.00000         1
  4   1_minTOF    20.000        NA        20.00000    20.00000    20.0000  21.00000         1
  79  2_n         637.0000    644.0000    460.0000    40.0       487.0000     NA            2
  80  2_meanTOF   171.6311    210.4984    176.0478    49.5       189.8439     NA            2
  81  2_medianTOF 155.0000    189.0000    81.5000     45.5       116.0000     NA            2
  82  2_minTOF    21.0000     20.0000      20.0000    21.0        20.0000     NA            2
  </code></pre>
  
  <p>I have been stumped on this for the past day. Notice the row numbers skip 1-4 … 79-82. Every 78 rows there is a split, the splits correspond to a different set. There are 30 sets of 78 for a grand total of 2340 rows, each set has the same row names except for a change in the set number (e.g #_name). Additionally there are a total of 97 columns, 96 of which have strain names in them and 1 (named L1) has a factor (1-30) that I have been using to try and split the data frame to do work on it (no success yet). Finally, and most importantly, Notice the placement of the NAs, specifically - they are in different columns for each set.</p>
  
  <p>This is what I would like to do with this data. </p>
  
  <p>1) Split df by factor column L1 into 30 individual sets (this can alternatively be done by splitting the data set by each 78 rows). I have been attempting this with both dplyr and plyr with no success.</p>
  
  <p>2) Remove columns that contain NA in ALL rows - this corresponds to a particular strain being lost from the dataset. Some columns have NAs for certain rows - I want to keep these.</p>
  
  <p>I have a function for this step that works, though I am unsure how to integrate it:</p>
  
  <pre><code>na.rm.fun &lt;- function(x){
    x[,colSums(is.na(x))&lt;nrow(x)]
    return(x)
  }
  </code></pre>
  
  <p>3) Remove Factor column "L1" because once split, I no longer need. This can be done prior to split if not using it to split. This is a straightforward command using:</p>
  
  <pre><code>&gt; select(df, -L1)
  </code></pre>
  
  <p>4) Save each individual set as a separate .Rda file. I attempted to create a for loop function to do this step, and I believe it is feasible, though I am at a loss on how to do assignments in a loop</p>
  
  <pre><code>save.fun &lt;- function(x){
    for(i in 1:30){
      z &lt;- x[(78*i-77):(78*i),]
      save(z, file = paste("pheno_plate_", i, ".Rda", sep = ""))
    }}
  save.fun(df)
  </code></pre>
  
  <p>When run on my initial df this splits it up in 78 row increments and saves the corresponding file, unfortunately I am unsure how to assign z_i through z_i+29 so I can load them up once saved. Additionally, I mad this loop prior to removing columns with all NAs, though I suppose I can do this afterwards by loading the 30 data.frames and running my remove NA function. I am curious how to make this work in dplyr</p>
  
  <p>Last note. If I change the save function script to this :</p>
  
  <pre><code>save.fun &lt;- function(x){
    for(i in 1:30){
      z &lt;- x[1:78,]
      save(z, file = paste("pheno_plate_", i, ".Rda", sep = ""))
    }}
  </code></pre>
  
  <p>And run dplyr like so:</p>
  
  <pre><code>df %.%
    group_by(L1) %.%
    do(na.rm.fun) %.%
    do(save.fun)
  </code></pre>
  
  <p>I notice two things :
  1) All of the saved files correspond to set 30, I assume this is because of the for loop
  2) (more importantly) ANY column that is completely NA in ANY set is completely removed from ALL file sets. I thought this was interesting and am wondering how to use dplyr in such a way to avoid this issue.</p>
  
  <p>As always, Any and all help is much appreciated. </p>