sapply - apply a function to each cell in a column of a dataframe in R -
edit @user5249203 pointing out geocoding best done ggmaps' geocode call. watch out na's though.
i struggling apply
family in r.
i using function takes in string , returns longitude , latitude
> ggeocode("philadelphia, pa") [1] 39.95258 -75.16522
i have simple dataframe has names of 52 states:
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" )
to practice apply
skills, want apply ggeocode
each cell in column of state_lat_long
dataframe.
couldn't simpler.
then problem this?
> view(apply(state_lat_long, function(x) ggeocode(x)))
when run this, get:
error in view : argument "fun" missing, no default
which don't understand, because fun
not missing.
so, let's try sapply
. it's supposed simple, right?
but wrong this?
view(sapply(state_lat_long$state, function(x) ggeocode(x)))
when run this, 2 rows 50 columns, packed na
s. can't make sense of it.
next, tried
view(apply(state_lat_long, 2, function(x) ggeocode(x)))
and got
state 40.71278 -74.00594
again, makes no sense!
what doing wrong? thanks.
i realise question *apply
, but, if after geocodes easier option use vectorised function, such ggmap::geocode
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" ) library(ggmap) ## make sure we're using correct geocode function call 'ggmap::geocode' ggmap::geocode(as.character(state_lat_long$state)) ... # lon lat # 1 -74.00594 40.71278 # 2 -116.41939 38.80261 # 3 -99.90181 31.96860 # 4 -119.41793 36.77826 # 5 -94.68590 46.72955 # 6 -101.00201 47.55149
Comments
Post a Comment