

Dear Rusers,
I am trying to add +1 to the cell value of certain cells in a raster. I have the cell numbers of the cells on which I'd like to perform said computation saved in a vector. I have a working code but with a large vector and raster it takes ages:
r < raster(ncol=5,nrow=5)
r[] < 0
vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
for(i in 1:length(vec))
{
r[vec[i]] < r[vec[i]] + 1
}
I have already tried to use r[vec] < r[vec] + 1 but that doesn't work properly, it just sets the values of the cells stored in the vector to 1. Does anybody know a faster way to do this ? Thanks in advance!
_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo


Hi,
I stub my toe on this all the time (and wish I didn't). The behavior isn't limited to raster  the replacement operator `[` in general doesn't update the left had side like my wild imagination might allow. It's just a function and so it must make a copy of the input.
This note seen here ?`[` sort of points out the issue... "Subassignment is done sequentially, so if an index is specified more than once the latest assigned value for an index will result." Between that, the phase of the moon and the passbyvalue stuff my brain starts to jumble.
On the other hand, if you have dependable indices and are simply incrementing you can leverage table() for your purpose.
x < rep(0,5)
i < c(1,1,1,4)
ti < table(i)
tindex < as.numeric(names(ti))
x[tindex] < x[tindex] + ti
Likewise for raster
library(raster)
r < raster(ncol=5,nrow=5)
r[] < 0
vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
tvec < table(vec)
tindex < as.integer(names(tvec))
r[tindex] < r[tindex] + tvec
I hope there are smarter/faster ways, but in the meantime it might speed things along for you.
Ben
> On Jul 10, 2016, at 3:34 PM, Giacomo May < [hidden email]> wrote:
>
> Dear Rusers,
> I am trying to add +1 to the cell value of certain cells in a raster. I have the cell numbers of the cells on which I'd like to perform said computation saved in a vector. I have a working code but with a large vector and raster it takes ages:
>
> r < raster(ncol=5,nrow=5)
> r[] < 0
> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
> for(i in 1:length(vec))
> {
> r[vec[i]] < r[vec[i]] + 1
> }
>
> I have already tried to use r[vec] < r[vec] + 1 but that doesn't work properly, it just sets the values of the cells stored in the vector to 1. Does anybody know a faster way to do this ? Thanks in advance!
>
> _______________________________________________
> RsigGeo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/rsiggeoBen Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.orgReport Gulf of Maine jellyfish sightings to [hidden email] 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/nickrecord/nickrecordlaboratory/mainejellies/_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo


Maybe something like this?
R> r < raster(nrows=5,ncols=5)
R> r[] < 0
R> idx < matrix(c(1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE) # four corners of
the raster!
R> r[idx] < r[idx] + 1
R> image(r)
>From R's ?`[` : When indexing arrays by '[' a single argument 'i' can be a
matrix with as many columns as there are dimensions of 'x'; the result is
then a vector with elements corresponding to the sets of indices in each
row of 'i'.
On Sun, Jul 10, 2016 at 5:59 PM, Ben Tupper < [hidden email]> wrote:
> Hi,
>
> I stub my toe on this all the time (and wish I didn't). The behavior
> isn't limited to raster  the replacement operator `[` in general doesn't
> update the left had side like my wild imagination might allow. It's just a
> function and so it must make a copy of the input.
>
> This note seen here ?`[` sort of points out the issue... "Subassignment is
> done sequentially, so if an index is specified more than once the latest
> assigned value for an index will result." Between that, the phase of the
> moon and the passbyvalue stuff my brain starts to jumble.
>
> On the other hand, if you have dependable indices and are simply
> incrementing you can leverage table() for your purpose.
>
> x < rep(0,5)
> i < c(1,1,1,4)
> ti < table(i)
> tindex < as.numeric(names(ti))
> x[tindex] < x[tindex] + ti
>
> Likewise for raster
>
> library(raster)
> r < raster(ncol=5,nrow=5)
> r[] < 0
> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
> tvec < table(vec)
> tindex < as.integer(names(tvec))
> r[tindex] < r[tindex] + tvec
>
> I hope there are smarter/faster ways, but in the meantime it might speed
> things along for you.
>
>
> Ben
>
>
> > On Jul 10, 2016, at 3:34 PM, Giacomo May < [hidden email]> wrote:
> >
> > Dear Rusers,
> > I am trying to add +1 to the cell value of certain cells in a raster. I
> have the cell numbers of the cells on which I'd like to perform said
> computation saved in a vector. I have a working code but with a large
> vector and raster it takes ages:
> >
> > r < raster(ncol=5,nrow=5)
> > r[] < 0
> > vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
> > for(i in 1:length(vec))
> > {
> > r[vec[i]] < r[vec[i]] + 1
> > }
> >
> > I have already tried to use r[vec] < r[vec] + 1 but that doesn't work
> properly, it just sets the values of the cells stored in the vector to 1.
> Does anybody know a faster way to do this ? Thanks in advance!
> >
> > _______________________________________________
> > RsigGeo mailing list
> > [hidden email]
> > https://stat.ethz.ch/mailman/listinfo/rsiggeo>
> 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 [hidden email] 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/nickrecord/nickrecordlaboratory/mainejellies/>
> _______________________________________________
> RsigGeo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/rsiggeo>
[[alternative HTML version deleted]]
_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo


Hi,
Interesting. How would you use the matrix approach to increment a particular cell more than once with a single subassignment? If I repeat the upper left raster coord in the matrix 3 times it still yields a value of 1 up there.
library(raster)
r < raster(nrows=5,ncols=5)
r[] < 0
idx < matrix(c(1,1,1,1,1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE)
r[idx] < r[idx] + 1
plot(r)
range(getValues(r))
#[1] 0 1
I could be misreading the original posting, but I think the desire is that the result will be increment for each repeated index using just one subassignment.
Cheers,
Ben
> On Jul 10, 2016, at 6:45 PM, Vijay Lulla < [hidden email]> wrote:
>
> Maybe something like this?
>
> R> r < raster(nrows=5,ncols=5)
> R> r[] < 0
> R> idx < matrix(c(1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE) # four corners of the raster!
> R> r[idx] < r[idx] + 1
> R> image(r)
>
> From R's ?`[` : When indexing arrays by '[' a single argument 'i' can be a matrix with as many columns as there are dimensions of 'x'; the result is then a vector with elements corresponding to the sets of indices in each row of 'i'.
>
>
> On Sun, Jul 10, 2016 at 5:59 PM, Ben Tupper < [hidden email] <mailto: [hidden email]>> wrote:
> Hi,
>
> I stub my toe on this all the time (and wish I didn't). The behavior isn't limited to raster  the replacement operator `[` in general doesn't update the left had side like my wild imagination might allow. It's just a function and so it must make a copy of the input.
>
> This note seen here ?`[` sort of points out the issue... "Subassignment is done sequentially, so if an index is specified more than once the latest assigned value for an index will result." Between that, the phase of the moon and the passbyvalue stuff my brain starts to jumble.
>
> On the other hand, if you have dependable indices and are simply incrementing you can leverage table() for your purpose.
>
> x < rep(0,5)
> i < c(1,1,1,4)
> ti < table(i)
> tindex < as.numeric(names(ti))
> x[tindex] < x[tindex] + ti
>
> Likewise for raster
>
> library(raster)
> r < raster(ncol=5,nrow=5)
> r[] < 0
> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
> tvec < table(vec)
> tindex < as.integer(names(tvec))
> r[tindex] < r[tindex] + tvec
>
> I hope there are smarter/faster ways, but in the meantime it might speed things along for you.
>
>
> Ben
>
>
> > On Jul 10, 2016, at 3:34 PM, Giacomo May < [hidden email] <mailto: [hidden email]>> wrote:
> >
> > Dear Rusers,
> > I am trying to add +1 to the cell value of certain cells in a raster. I have the cell numbers of the cells on which I'd like to perform said computation saved in a vector. I have a working code but with a large vector and raster it takes ages:
> >
> > r < raster(ncol=5,nrow=5)
> > r[] < 0
> > vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
> > for(i in 1:length(vec))
> > {
> > r[vec[i]] < r[vec[i]] + 1
> > }
> >
> > I have already tried to use r[vec] < r[vec] + 1 but that doesn't work properly, it just sets the values of the cells stored in the vector to 1. Does anybody know a faster way to do this ? Thanks in advance!
> >
> > _______________________________________________
> > RsigGeo mailing list
> > [hidden email] <mailto: [hidden email]>
> > https://stat.ethz.ch/mailman/listinfo/rsiggeo < https://stat.ethz.ch/mailman/listinfo/rsiggeo>
>
> 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 [hidden email] <mailto: [hidden email]> 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/nickrecord/nickrecordlaboratory/mainejellies/ < https://www.bigelow.org/research/srs/nickrecord/nickrecordlaboratory/mainejellies/>
>
> _______________________________________________
> RsigGeo mailing list
> [hidden email] <mailto: [hidden email]>
> https://stat.ethz.ch/mailman/listinfo/rsiggeo < https://stat.ethz.ch/mailman/listinfo/rsiggeo>
>
Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.orgReport Gulf of Maine jellyfish sightings to [hidden email] 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/nickrecord/nickrecordlaboratory/mainejellies/ [[alternative HTML version deleted]]
_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo


OOPS! I appear to have misunderstood the question then, Ben. I'm unsure
how to increment a particular cell more than once.
Please pardon the noise.
On Sun, Jul 10, 2016 at 7:54 PM, Ben Tupper < [hidden email]> wrote:
> Hi,
>
> Interesting. How would you use the matrix approach to increment a
> particular cell more than once with a single subassignment? If I repeat
> the upper left raster coord in the matrix 3 times it still yields a value
> of 1 up there.
>
> library(raster)
> r < raster(nrows=5,ncols=5)
> r[] < 0
> idx < matrix(c(1,1,1,1,1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE)
> r[idx] < r[idx] + 1
> plot(r)
> range(getValues(r))
> #[1] 0 1
>
> I could be misreading the original posting, but I think the desire is that
> the result will be increment for each repeated index using just one
> subassignment.
>
> Cheers,
> Ben
>
>
> On Jul 10, 2016, at 6:45 PM, Vijay Lulla < [hidden email]> wrote:
>
> Maybe something like this?
>
> R> r < raster(nrows=5,ncols=5)
> R> r[] < 0
> R> idx < matrix(c(1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE) # four corners of
> the raster!
> R> r[idx] < r[idx] + 1
> R> image(r)
>
> From R's ?`[` : When indexing arrays by '[' a single argument 'i' can be a
> matrix with as many columns as there are dimensions of 'x'; the result is
> then a vector with elements corresponding to the sets of indices in each
> row of 'i'.
>
>
> On Sun, Jul 10, 2016 at 5:59 PM, Ben Tupper < [hidden email]> wrote:
>
>> Hi,
>>
>> I stub my toe on this all the time (and wish I didn't). The behavior
>> isn't limited to raster  the replacement operator `[` in general doesn't
>> update the left had side like my wild imagination might allow. It's just a
>> function and so it must make a copy of the input.
>>
>> This note seen here ?`[` sort of points out the issue... "Subassignment
>> is done sequentially, so if an index is specified more than once the latest
>> assigned value for an index will result." Between that, the phase of the
>> moon and the passbyvalue stuff my brain starts to jumble.
>>
>> On the other hand, if you have dependable indices and are simply
>> incrementing you can leverage table() for your purpose.
>>
>> x < rep(0,5)
>> i < c(1,1,1,4)
>> ti < table(i)
>> tindex < as.numeric(names(ti))
>> x[tindex] < x[tindex] + ti
>>
>> Likewise for raster
>>
>> library(raster)
>> r < raster(ncol=5,nrow=5)
>> r[] < 0
>> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
>> tvec < table(vec)
>> tindex < as.integer(names(tvec))
>> r[tindex] < r[tindex] + tvec
>>
>> I hope there are smarter/faster ways, but in the meantime it might speed
>> things along for you.
>>
>>
>> Ben
>>
>>
>> > On Jul 10, 2016, at 3:34 PM, Giacomo May < [hidden email]> wrote:
>> >
>> > Dear Rusers,
>> > I am trying to add +1 to the cell value of certain cells in a raster. I
>> have the cell numbers of the cells on which I'd like to perform said
>> computation saved in a vector. I have a working code but with a large
>> vector and raster it takes ages:
>> >
>> > r < raster(ncol=5,nrow=5)
>> > r[] < 0
>> > vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
>> > for(i in 1:length(vec))
>> > {
>> > r[vec[i]] < r[vec[i]] + 1
>> > }
>> >
>> > I have already tried to use r[vec] < r[vec] + 1 but that doesn't work
>> properly, it just sets the values of the cells stored in the vector to 1.
>> Does anybody know a faster way to do this ? Thanks in advance!
>> >
>> > _______________________________________________
>> > RsigGeo mailing list
>> > [hidden email]
>> > https://stat.ethz.ch/mailman/listinfo/rsiggeo>>
>> 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 [hidden email] 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/nickrecord/nickrecordlaboratory/mainejellies/>>
>> _______________________________________________
>> RsigGeo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/rsiggeo>>
>
>
> 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 [hidden email]
> 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/nickrecord/nickrecordlaboratory/mainejellies/>
>
[[alternative HTML version deleted]]
_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo


This works fine for me:
library(raster)
r < raster(ncol=5,nrow=5)
r[] < 0
vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
a < data.frame(table(vec))
a$vec < as.numeric(a$vec) # Not very elegant coercion, there must be a
better way
r[a$vec] < r[a$vec] + a$Freq[a$vec]
Cheers,
Loïc
On 07/11/2016 02:58 AM, Vijay Lulla wrote:
> OOPS! I appear to have misunderstood the question then, Ben. I'm unsure
> how to increment a particular cell more than once.
>
> Please pardon the noise.
>
> On Sun, Jul 10, 2016 at 7:54 PM, Ben Tupper < [hidden email]> wrote:
>
>> Hi,
>>
>> Interesting. How would you use the matrix approach to increment a
>> particular cell more than once with a single subassignment? If I repeat
>> the upper left raster coord in the matrix 3 times it still yields a value
>> of 1 up there.
>>
>> library(raster)
>> r < raster(nrows=5,ncols=5)
>> r[] < 0
>> idx < matrix(c(1,1,1,1,1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE)
>> r[idx] < r[idx] + 1
>> plot(r)
>> range(getValues(r))
>> #[1] 0 1
>>
>> I could be misreading the original posting, but I think the desire is that
>> the result will be increment for each repeated index using just one
>> subassignment.
>>
>> Cheers,
>> Ben
>>
>>
>> On Jul 10, 2016, at 6:45 PM, Vijay Lulla < [hidden email]> wrote:
>>
>> Maybe something like this?
>>
>> R> r < raster(nrows=5,ncols=5)
>> R> r[] < 0
>> R> idx < matrix(c(1,1,1,5,5,1,5,5),ncol=2,byrow=TRUE) # four corners of
>> the raster!
>> R> r[idx] < r[idx] + 1
>> R> image(r)
>>
>> From R's ?`[` : When indexing arrays by '[' a single argument 'i' can be a
>> matrix with as many columns as there are dimensions of 'x'; the result is
>> then a vector with elements corresponding to the sets of indices in each
>> row of 'i'.
>>
>>
>> On Sun, Jul 10, 2016 at 5:59 PM, Ben Tupper < [hidden email]> wrote:
>>
>>> Hi,
>>>
>>> I stub my toe on this all the time (and wish I didn't). The behavior
>>> isn't limited to raster  the replacement operator `[` in general doesn't
>>> update the left had side like my wild imagination might allow. It's just a
>>> function and so it must make a copy of the input.
>>>
>>> This note seen here ?`[` sort of points out the issue... "Subassignment
>>> is done sequentially, so if an index is specified more than once the latest
>>> assigned value for an index will result." Between that, the phase of the
>>> moon and the passbyvalue stuff my brain starts to jumble.
>>>
>>> On the other hand, if you have dependable indices and are simply
>>> incrementing you can leverage table() for your purpose.
>>>
>>> x < rep(0,5)
>>> i < c(1,1,1,4)
>>> ti < table(i)
>>> tindex < as.numeric(names(ti))
>>> x[tindex] < x[tindex] + ti
>>>
>>> Likewise for raster
>>>
>>> library(raster)
>>> r < raster(ncol=5,nrow=5)
>>> r[] < 0
>>> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
>>> tvec < table(vec)
>>> tindex < as.integer(names(tvec))
>>> r[tindex] < r[tindex] + tvec
>>>
>>> I hope there are smarter/faster ways, but in the meantime it might speed
>>> things along for you.
>>>
>>>
>>> Ben
>>>
>>>
>>>> On Jul 10, 2016, at 3:34 PM, Giacomo May < [hidden email]> wrote:
>>>>
>>>> Dear Rusers,
>>>> I am trying to add +1 to the cell value of certain cells in a raster. I
>>> have the cell numbers of the cells on which I'd like to perform said
>>> computation saved in a vector. I have a working code but with a large
>>> vector and raster it takes ages:
>>>>
>>>> r < raster(ncol=5,nrow=5)
>>>> r[] < 0
>>>> vec < c(1,1,1,3,4,5,6,7,8,8,8,9)
>>>> for(i in 1:length(vec))
>>>> {
>>>> r[vec[i]] < r[vec[i]] + 1
>>>> }
>>>>
>>>> I have already tried to use r[vec] < r[vec] + 1 but that doesn't work
>>> properly, it just sets the values of the cells stored in the vector to 1.
>>> Does anybody know a faster way to do this ? Thanks in advance!
>>>>
>>>> _______________________________________________
>>>> RsigGeo mailing list
>>>> [hidden email]
>>>> https://stat.ethz.ch/mailman/listinfo/rsiggeo>>>
>>> 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 [hidden email] 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/nickrecord/nickrecordlaboratory/mainejellies/>>>
>>> _______________________________________________
>>> RsigGeo mailing list
>>> [hidden email]
>>> https://stat.ethz.ch/mailman/listinfo/rsiggeo>>>
>>
>>
>> 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 [hidden email]
>> 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/nickrecord/nickrecordlaboratory/mainejellies/>>
>>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> RsigGeo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/rsiggeo>
_______________________________________________
RsigGeo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/rsiggeo

