title: How should I efficiently label the rows of a dataframe by value?
body: <p>I have a dataframe of trips with a column of how the trip was made similar to column "a" below. </p>
  
  <p>My goal is to add a new column of data to each row according to the value of column "a". </p>
  
  <p>The function below works, but seems like an overly complicated way of accomplishing a fairly simple objective. </p>
  
  <p><code>new_var &lt;- 0</code> seems likely to cause a bug. </p>
  
  <p>If I don't attach the dataframe and call the column that way the function does not work, which is obviously not great. </p>
  
  <p>Is there an obviously better way to do this? </p>
  
  <pre><code>a &lt;- c("On the Subway", "On a train", "Driving","On the Subway", "On a train", "Driving","On the Subway", "On a train", "Driving")
  b &lt;- c(1,15,3,54,5,32,7,76,9)
  my_dataframe &lt;- data.frame(a,b)
  
  new_var &lt;- 0
  carbon_func &lt;- function(variable){
  
    new_var[variable == 'On the subway'] &lt;- 0.014
    new_var[variable == 'On a train'] &lt;- 0.014
    new_var[variable == 'On a bus'] &lt;- 0.068
    new_var[variable == 'Motorcycling'] &lt;- 0.072
    new_var[variable == 'Driving'] &lt;- 0.12
    new_var[variable == 'In a taxi or rideshare'] &lt;- 0.12
    new_var[variable == 'Flying'] &lt;- 0.285
    new_var[variable == 'Cycling'] &lt;- 0
    new_var[variable == 'Walking'] &lt;- 0
  
    return(new_var)
  
  }
  
  attach(my_dataframe)
  my_dataframe$Carbon_Footprint &lt;- carbon_func(a)
  detach(my_dataframe)
  </code></pre>
  
  <p>I've explored using a dictionary type thing like below but haven't found a working  alternative to what is posted </p>
  
  <pre><code>standardized_footprints &lt;- c(0.014, 0.014, 0.068, 0.072, 0.12, 0.12, 0.285)
  names(standardized_footprints) &lt;- c('On the subway', 'On a train', 'On a bus', 'Motorcycling', 'Driving', 'In a taxi or rideshare', 'Flying')
  </code></pre>
  
  <p>Thanks!</p>