[R-sig-Geo] Selecting a range of longitude and latitudes
r@i@1290 m@iii@g oii @im@com
r@i@1290 m@iii@g oii @im@com
Sat Apr 20 01:24:58 CEST 2019
Hi Mike (and everyone),
Thanks for this reply!
Let's say that I wanted to average the values of every grid cell within, say, 103 to 116 degrees longitude and 5 to 16 degrees latitude. Would there be a way to select all of those grid cells to average them? I've done something like this to already compute the averages across all grid cells based on values :
RCP1pctCO2ModelMean <- (Model2 + Model10 + Model18 + Model26 + subset14 + Model42 + subset20 + subset24 + Model60 + Model68) / 10 ###This provides an average value across each of the 140 layers for each of the 8192 grid cells based on these 10 models (i.e. all of the layer 1s are averaged for each model for the first grid cell, then all of the layer 2s are averaged for the first grid cell.....all the way to layer 140. Then, this is repeated for the second grid cell....all the way to grid cell 8192). This way, I have 140 averages across all grid cells. :)
So, to average values ONLY across a selected range of grid cells, is there a way to do that?
Thanks, again,
-----Original Message-----
From: Michael Sumner <mdsumner using gmail.com>
To: rain1290 <rain1290 using aim.com>
Sent: Fri, Apr 19, 2019 6:36 pm
Subject: Re: [R-sig-Geo] Selecting a range of longitude and latitudes
You want expand.grid(get, Hope2) to get every coordinate, but
coordinates(Model2) ## gives a matrix (topleft, across rows, then
down same as values(Model2))
is far easier, albeit having been through layers of interpretation of
the original arrays in the file.
All the best, Mike
On Fri, Apr 19, 2019 at 11:51 AM rain1290--- via R-sig-Geo
<r-sig-geo using r-project.org> wrote:
>
> Hi Ben (and everyone),
>
> Ah, yes, you're right - using the suggestion that you specified, that error message disappeared! So, if I understand, this would take all of the grid cells within those specified longitude and latitude ranges (i.e. that entire area)?
> That said, when I try plotting this using the following:
> get <- ncvar_get(Model1, "cum_co2_emi-CanESM2") #for x-axis (140 values)
> Model2 <- brick("MaxPrecCCCMACanESM21pctCO2.nc", var="onedaymax") #for y-axis (140 values)
>
> Hope2 <- extract(Model2, lonlat)
>
> plot(get,Hope2, type="l",col="green", lwd="3", xlab="Cumulative emissions (TtC)", ylab="One-day maximum precipitation (mm/day)", main="CanESM2")
> I receive this error:
> Error in xy.coords(x, y, xlabel, ylabel, log) :
> 'x' and 'y' lengths differ
> *Note that I do not have this problem when specifying a "specific" longitude and latitude, rather than a range. Is there any reason for this?
> Thanks, again
> -----Original Message-----
> From: Ben Tupper <btupper using bigelow.org>
> To: rain1290 <rain1290 using aim.com>
> Cc: r-sig-geo <r-sig-geo using r-project.org>
> Sent: Thu, Apr 18, 2019 9:26 pm
> Subject: Re: [R-sig-Geo] Selecting a range of longitude and latitudes
>
> Hi,
> The example you give is different than the one listed in the error message. In your example you bind two 14-element vectors into a matrix of two columns.
> cbind(103:116, 3:16)
> But in the error you show the first vector is only 8 elements long...
> cbind(103:110, 3:16)
> When R encounters an attempt to bind columns of unequal length it will try to "recycle" the shorter vector as long as it is a multiple of the longer. In this case of binding an 8-element vector and a 14-element vector it fails.
> Note that shortening up the first vector to 7 does work since 7 is a multiple of 14...
> cbind(103:109, 3:16) [,1] [,2] [1,] 103 3 [2,] 104 4 [3,] 105 5 [4,] 106 6 [5,] 107 7 [6,] 108 8 [7,] 109 9 [8,] 103 10 [9,] 104 11[10,] 105 12[11,] 106 13[12,] 107 14[13,] 108 15[14,] 109 16
>
> Note how the sequence 103...109 appears twice in the first column. Recycling is nice (and handy) until you get surprised by it.
> Cheers,Ben
>
> On Apr 18, 2019, at 8:37 PM, rain1290--- via R-sig-Geo <r-sig-geo using r-project.org> wrote:
> Hi there,
> I am trying to specify a range of longitudes and latitudes. I tried this using the following:
>
>
> lonlat <- cbind(103:116, 3:16) #This would specify a range of longitudes and latitudes
>
> However, I receive the following warning message:
> Warning message:
> In cbind(103:110, 3:16) :
> number of rows of result is not a multiple of vector length (arg 1)
>
> When I specify the longitude and latitude of a specific location, though, it works just fine, like this:
> lonlat <- cbind(103, 3)
>
> Am I specifying the ranges of coordinates incorrectly? The warning message would suggest yes, but I do not understand why.
> Thanks, and any help with this would be greatly appreciated!
> ~Trav.~
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using 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
> Ecological Forecasting: https://eco.bigelow.org/
>
>
>
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
--
Michael Sumner
Software and Database Engineer
Australian Antarctic Division
Hobart, Australia
e-mail: mdsumner using gmail.com
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list