title: Anonymous function in lapply
body: <p>I am reading Wickham's Advanced R book. This question is relating to solving Question 5 in chapter 12 - Functionals. The exercise asks us to:</p>
  
  <blockquote>
    <p>Implement a version of <code>lapply()</code> that supplies FUN with both the name and value of each component.</p>
  </blockquote>
  
  <p>Now, when I run below code, I get expected answer for one column.</p>
  
  <pre><code>c(class(iris[1]),names(iris[1]))
  </code></pre>
  
  <p>Output is: </p>
  
  <pre><code>"data.frame"   "Sepal.Length"
  </code></pre>
  
  <p>Building upon above code, here's what I did:</p>
  
  <pre><code>lapply(iris,function(x){c(class(x),names(x))})
  </code></pre>
  
  <p>However, I only get the output from <code>class(x)</code> and not from <code>names(x)</code>. Why is this the case? </p>
  
  <p>I also tried <code>paste()</code> to see whether it works.</p>
  
  <pre><code>lapply(iris,function(x){paste(class(x),names(x),sep = " ")})
  </code></pre>
  
  <p>I only get <code>class(x)</code> in the output. I don't see <code>names(x)</code> being returned. </p>
  
  <p>Why is this the case? Also, how do I fix it?</p>
  
  <p>Can someone please help me?</p>