[R-sig-eco] automating ações

Philippi, Tom tom_philippi at nps.gov
Tue Sep 15 20:52:16 CEST 2015


Katia--
Your attached script didn't come through. as attachments are stripped by
the mailing list.

However, I can give you 2 generic answers.

First, if your vector of species names is called SpList, and your data are
in a big SpatialPointsDataFrame spdf

for (SP in SpList) {
     spSP <- as(spdf[spdf$Species==SP,],'ppp')  # subset to just that
species and return as ppp object
    temp1 <- bw.smoothppp(spSP,...)
    temp2 <- nextstep(temp1)
    tempplot <- plot(...)
    # probably display or write to a file
    png(filename=paste(SP,'smootheddensity.png',sep=''))
    print(tempplot)
    dev.off()
} # bottom for SP in SpList

That structure works well for things like hitting gbif, that need to be
done sequentially.  It can work for your use case, but will be slow, and is
considered poor R "style".

In general, in R, one should not use for loops, but rather let R vectorize
in line via one of the *apply functions or even more powerful tools in plyr
or other packages.  The "better" R style is to write a small wrapper
function DoPP() that takes a species name and spatial object as parameters,
performs the steps you want for each species, then returns the graph
object.  Then, you can use dlply() in the plyr package to apply that
function to each species, and return a list of graph objects.

I hope that this points you in a useful direction.

Tom 2
"If you find that you're spending almost all your time on theory, start
turning some attention to practical things; it will improve your theories.
If you find that you're spending almost all your time on practice, start
turning some attention to theoretical things; it will improve your
practice."
  -- Donald Knuth



On Tue, Sep 15, 2015 at 11:01 AM, Kátia Emidio <kat.emidio at gmail.com> wrote:

> Dear all,
>
> I´m a beginner in using FOR in order to create a loop in R to automate
> ações in it.
>
> I have a script, attached, and I need to perform it for all species in the
> file  (test_richness). The package I'm using is the SpatStat. The loop
> needs to be created before the "bw.smoothppp" function (line 45), which get
> the sigma value, that is used as a parameter for the  "plot(Smooth...)..
> and the image needs to be automatic generated as a file.
> I'll appreciate any help!
> Cheers,
>
> --
> Kátia Emídio da Silva DSc
> Eng. Florestal
> Manaus/AM
>
>
>
> Forestry Engineer
> Manaus/AM-Brazil
> _______________________________________________
> R-sig-ecology mailing list
> R-sig-ecology at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

	[[alternative HTML version deleted]]



More information about the R-sig-ecology mailing list