[R-sig-Geo] Add +1 to current value of a cell in a raster in R
Ben Tupper
btupper at bigelow.org
Mon Jul 11 23:33:04 CEST 2016
Hi,
dplyr is faster for larger datasets compared to using table. I tried adjusting the value of n below and for larger n (my typical situtation) dplyr stayed very fast while tablulate/table took more time.
A good lesson for me - start using dplyr everyday!
Thanks,
Ben
library(raster)
library(dplyr)
# @param r raster
# @param cells cell indices to be incremented, repeats are honored
# @return updated raster
increment_by_tabulate <- function(r, cells){
n <- ncell(r)
tbl <- tabulate(cells, n)
tcell <- 1:n
r[tcell] <- r[tcell] + tbl
r
}
# @param r raster
# @param cells cell indices to be incremented, repeats are honored
# @return updated raster
increment_by_table <- function(r, cells){
tbl <- table(cells)
tcell <- as.integer(names(tbl))
r[tcell] <- r[tcell] + tbl
r
}
# @param r raster
# @param cells cell indices to be incremented, repeats are honored
# @return updated raster
increment_by_dplyr <- function(r, cells){
cel <- data_frame(cell = cells)
tab <- cel %>% group_by(cell) %>% summarize(val = n())
r[tab$cell] <- tab$val
r
}
n <- 500
r <- raster(ncol=n,nrow=n)
r[] <- 0
cells <- sample(1:(n*n), 2*n*n, replace = TRUE)
system.time( r1 <- increment_by_tabulate(r, cells) )
system.time( r2 <- increment_by_table(r, cells) )
system.time( r3 <- increment_by_dplyr(r, cells) )
identical(r1,r2)
identical(r1,r3)
> On Jul 11, 2016, at 1:21 PM, Giacomo May <Giacomo_May94 at gmx.de> wrote:
>
> Hi Ben,
> I am happy if I can be of help. So I have tried the two suggestions (one from stackexchange, which i provided, and one from the mailing list, from Loic to be exact) and the only one working properly was the one from stackexchange. it was very fast, it took 1 second max. If you want me to try anything else out for you just let me know.
> best,
> Giacomo
>
> Gesendet: Montag, 11. Juli 2016 um 18:33 Uhr
> Von: "Ben Tupper" <btupper at bigelow.org>
> An: "Giacomo May" <Giacomo_May94 at gmx.de>
> Cc: r-sig-geo at r-project.org
> Betreff: Re: [R-sig-Geo] Add +1 to current value of a cell in a raster in R
> Hi Giacomo,
>
> I am interested to know which you find fastest - perhaps you could let us know some timing results?
>
> Thanks,
> Ben
>
> On Jul 11, 2016, at 12:21 PM, Giacomo May <Giacomo_May94 at gmx.de <>> wrote:
>
> Thanks for all the replies. In case anyone is interested, I asked the same question on stackexchange and got the following answer:
>
> library(dplyr)
> cel <- data_frame(cell = vec)
> tab <- cel %>% group_by(cell) %>% summarize(val = n())
> ## update your raster
> r[tab$cell] <- tab$val
>
> I will try the soultions you guys suggested as well, thanks a lot again !
> Best,
> Giacomo
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org <>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo <https://stat.ethz.ch/mailman/listinfo/r-sig-geo>
> Ben Tupper
> Bigelow Laboratory for Ocean Sciences
> 60 Bigelow Drive, P.O. Box 380
> East Boothbay, Maine 04544
> http://www.bigelow.org <http://www.bigelow.org/>
>
> Report Gulf of Maine jellyfish sightings to jellyfish at bigelow.org <> or tweet them to #MaineJellies -- include date, time, and location, as well as any descriptive information such as size or type. Learn more at https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/ <https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/>_______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org
Report Gulf of Maine jellyfish sightings to jellyfish at bigelow.org or tweet them to #MaineJellies -- include date, time, and location, as well as any descriptive information such as size or type. Learn more at https://www.bigelow.org/research/srs/nick-record/nick-record-laboratory/mainejellies/
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list