title: "Error in [...] : attempt to select more than one element in vectorIndex" problem when trying to run lapply(). Can anyone work with me to debug this?
body: <p>I currently have this function that returns the mean life expectancy of a given year:</p>
  
  <pre><code>get_col_mean &lt;- function(data_frame, column_name){
    return(mean(data_frame[[column_name]], na.rm = TRUE))
  }
  </code></pre>
  
  <p>An example of it's use, if I was to run the code:</p>
  
  <pre><code>test2 &lt;- get_col_mean(life_exp, "X2000")
  </code></pre>
  
  <p>I receive <strong>67.486</strong>, which is the average life expectancy worldwide in the year 2000.</p>
  
  <p>Now, I am trying to use the following code:</p>
  
  <pre><code>col_means &lt;- lapply(life_exp, get_col_mean, paste("X", 1800:2100))
  </code></pre>
  
  <p>To create a <strong>list</strong> of life expectancy means for every year. However, when I try to run this line of code, it gives me error:</p>
  
  <pre><code>Error in data_frame[[column_name]] : attempt to select more than one element in vectorIndex
  </code></pre>
  
  <p>Can anyone help me figure out what I'm doing wrong? Also I know that I can solve this using different methods a lot easier (that don't require my function), but for the sake of my assignment, I have to use my function to solve this problem. The assignment wants me to use apply(), lapply(), or sapply() to solve this problem, so if apply() or sapply() would work better than lapply(), let me know that too. Also, if the only way to solve this problem is to alter my function in some way (my function is the problem, not my lapply() code), I am fine with doing so.</p>
  
  <p>I'm trying to attach a picture of what the life_exp data.frame looks like but I can't find any way to do so (first time using this website). In an attempt to explain it, all of the rows represent a country and all of the columns represent a year (X1900 being the year 1900). Row "Afghanistan" + column "X1900" would give a number representing the life expectancy of a person in Afghanistan in the year 1900.</p>