title: Using Apply or Vectorize to apply custom function to a dataframe
body: <p>I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want:</p>
  
  <pre><code>celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"),
                         age=c(28,23,49,29,38,23,29),
                         income=c(25.2,10.5,11,21.9,44,11.5,45))
  f=function(x,output){
    name=x[1]
    income=x[3]
    cat(name,income,"\n")
  }
  apply(celebrities,1,f)
  </code></pre>
  
  <p>But when I try to take it and apply mathematical function it doesn't work:</p>
  
  <pre><code>  f2=function(x,output){
    age=x[2]
    income=x[3]
    sum(age,income)
  }
  apply(celebrities,1,f2)
  </code></pre>
  
  <p>In essence what I need is for apply to take a dataset, go through every row of that dataset using the values in that row as inputs into the function and add a third column to the dataset with the results of the function. Please let me know how I can clarify this question if needed. I have referred to the questions below, but they don't seem to work for me.</p>
  
  <p><a href="https://stackoverflow.com/questions/4236368/apply-a-function-to-every-row-of-a-matrix-or-a-data-frame">Apply a function to every row of a matrix or a data frame</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28820505/how-to-assign-new-values-from-lapply-to-new-column-in-dataframes-in-list">How to assign new values from lapply to new column in dataframes in list</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15059076/call-apply-like-function-on-each-row-of-dataframe-with-multiple-arguments-from-e">Call apply-like function on each row of dataframe with multiple arguments from each row</a></p>