title: apply a function to each cell in a column of a dataframe in R
body: <p><strong>EDIT</strong> Thanks to @user5249203 for pointing out that geocoding is best done with ggmaps' geocode call. Watch out for NA's though.  </p>
  
  <p>I am struggling with the <code>apply</code> family in R.  </p>
  
  <p>I am using a <a href="https://stackoverflow.com/questions/3257441/geocoding-in-r-with-google-maps">function</a> which takes in a string and returns longitude and latitude</p>
  
  <p><code>&gt; gGeoCode("Philadelphia, PA")
  [1]  39.95258 -75.16522</code></p>
  
  <p>I have a simple dataframe which has the names of all 52 states:  </p>
  
  <pre><code>dput(state_lat_long)
  structure(
    list(State = structure(
      c(
        32L, 28L, 43L, 5L, 23L, 34L,
        30L, 13L, 14L, 38L, 22L, 25L, 15L, 20L, 24L, 40L, 46L, 21L, 9L,
        18L, 48L, 10L, 7L, 4L, 3L, 31L, 35L, 37L, 49L, 44L, 12L, 6L,
        17L, 36L, 11L, 39L, 42L, 8L, 47L, 33L, 16L, 1L, 29L, 27L, 26L,
        19L, 41L, 50L, 2L, 45L
      ), .Label = c(
        "alabama", "alaska", "arizona",
        "arkansas", "california", "colorado", "connecticut", "delaware",
        "florida", "georgia", "hawaii", "idaho", "illinois", "indiana",
        "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland",
        "massachusetts", "michigan", "minnesota", "mississippi", "missouri",
        "montana", "nebraska", "nevada", "new hampshire", "new jersey",
        "new mexico", "new york", "north carolina", "north dakota", "ohio",
        "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina",
        "south dakota", "tennessee", "texas", "utah", "vermont", "virginia",
        "washington", "west virginia", "wisconsin", "wyoming"
      ), class = "factor"
    )), .Names = "State", row.names = c(NA,-50L), class = "data.frame"
  )
  </code></pre>
  
  <p>To practice my <code>apply</code> skills, I simply want to apply <code>gGeoCode</code> to each cell in the only column of the <code>state_lat_long</code> dataframe.  </p>
  
  <p>Couldn't be much simpler.  </p>
  
  <p>Then what is the problem with this?  </p>
  
  <pre><code>&gt; View(apply(state_lat_long, function(x) gGeoCode(x)))
  </code></pre>
  
  <p>When I run this, I get:  </p>
  
  <pre><code>Error in View : argument "FUN" is missing, with no default  
  </code></pre>
  
  <p>which I don't understand, because <code>FUN</code> is not missing.  </p>
  
  <p>So, let's try <code>sapply</code>.  It's supposed to be simple, right?  </p>
  
  <p>But what is wrong with this?</p>
  
  <pre><code>View(sapply(state_lat_long$State, function(x) gGeoCode(x)))
  </code></pre>
  
  <p>When I run this, I get 2 rows with 50 columns, packed with <code>NA</code>s. I can't make sense of it.  </p>
  
  <p>Next, I tried  </p>
  
  <pre><code>View(apply(state_lat_long, 2, function(x) gGeoCode(x)))  
  </code></pre>
  
  <p>and I got  </p>
  
  <pre><code>     State
    40.71278
   -74.00594  
  </code></pre>
  
  <p>Again, this makes no sense!  </p>
  
  <p>What am I doing wrong? Thanks.  </p>