title: loop over levels of a factor variable in a function
body: <p>I have a dataframe, dat, with a covariate site coded as a factor with 31 different levels.</p>
  
  <pre><code>cas_1_sitea_586754968 0 0 1 2 0 sitea 
  con_65_sitea_568859302 1 0 2 1 1 siteb
  cas_9_siteb_0799700 0 0 0 0 0 siteb 
  con_siteb_THR84569 2 0 0 1 0 sitea
  </code></pre>
  
  <p>I have a function that works when I apply it to one site variable at a time:</p>
  
  <pre><code>get_maf &lt;- function(data){
      allele.count &lt;- apply(data[,1:(ncol(data)-2)],2,sum)
      maf &lt;- allele.count/(2*nrow(data))
      out &lt;- paste((unique(data$site)),"_jp.maf",sep="")
      write.table(maf, out, col.names=F, quote=F)
  }
  </code></pre>
  
  <p>But, when I try to loop over the data within each of the 31 sites using lapply like this: </p>
  
  <pre><code>lapply(unique(dat$site), get_maf, data = dat)    
  </code></pre>
  
  <p>I get an error: <code>lapply(unique(jp$site), get_maf_jp, data = jp)
  Error in FUN(c("aber", "ajsz", "asrb", "buls", "cati", "caws", "cims",  : 
    unused argument (c("aber", "ajsz", "asrb", "buls", "cati", "caws", "cims", "clo3", "cou3", "denm", "dubl", "edin", "egcu", "ersw", "gras", "irwt", "lie2", "lie5", "mgs2", "msaf", "munc", "pewb", "pews", "s234", "swe1", "swe5", "swe6", "top8", "ucla", "umeb", "umes")[[1]])</code></p>
  
  <p>Any insights into what I am doing wrong here are greatly appreciated.</p>