[R] lapply , mapply questions

Michael Denslow michael.denslow at gmail.com
Tue Dec 15 04:05:56 CET 2009


Hi Matt,

On Mon, Dec 14, 2009 at 5:49 AM, Bunny, lautloscrew.com
<bunny at lautloscrew.com> wrote:
> Dear all,
>
> i have a programming problem that should be simple, though i am stuck with it. Please note that this is not a specific geonames problem, though i use it as an example - it愀 just a basic problem with lapply.
>
> I use the geonames webservices with the R geonames packages which works almost smoothly.
> I have a vector of Zipcodes and i want to do a geonames lookup for all of them, which should not be a problem.
> Of course i could create a loop for that one, but i think lapply should be possible, too.
>
> The problem is to hand over the variable to the lookup function.
>
> lapply(zipcodes,GNpostalCodeLookup(postalcode=zipcodes,country="US"))
>
> where zipcodes is my vector of zip codes an GNpostalCodeLookup is the function that gives back some information like longitude and aptitude etc. Is it possible to use lapply in that context or can I just use it in contexts like lapply(somelistofvectors, mean) ?
>
> all i悲 like to have is some data.frame like this
>
> zip     apt    long
> 1234  xy.x        xx.x
> 1235  ax.x   aa.a

I am not sure what aptitude is, but this might help.
Note that this is a reproducible example.

library(geonames)
# make fake dataframe
z.df <- data.frame(zip = c(28607, 28608, 28609),country = c('US','US','US'))

# add longitude to dataframe
z.df$long=apply(z.df,1,function(l){GNpostalCodeLookup(postalcode =
l[1], country = l[2])$lng})
# add latitude to dateframe
z.df$lat=apply(z.df,1,function(l){GNpostalCodeLookup(postalcode =
l[1], country = l[2])$lat})

I am sure there is a way to get this into one function but this is
what I came up with.

Hope this helps,
Michael

>
> thx in advance
>
> matt
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Michael Denslow

Graduate Student & Adjunct Instructor
I.W. Carpenter Jr. Herbarium [BOON]
Department of Biology
Appalachian State University
Boone, North Carolina U.S.A.
-- AND --
Communications Manager
Southeast Regional Network of Expertise and Collections
sernec.org

36.214177, -81.681480 +/- 3103 meters




More information about the R-help mailing list