Modifying the length of a matrix variable

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Modifying the length of a matrix variable

R-sig-geo mailing list
Hello there,
I am currently trying to modify a variable's length. It is called "Model4" and is a matrix. It currently has the length of 95, as per "length(Model4)". However, I would like to create a new Model4 (let's say "NewModel4"), in which it has a length of 90, instead of 95.
Is there a way to do this? 
Thanks, and any assistance would be greatly appreciated!  
        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Rolf Turner

On 31/03/19 3:56 AM, rain1290--- via R-sig-Geo wrote:

> Hello there, I am currently trying to modify a variable's length. It
> is called "Model4" and is a matrix. It currently has the length of
> 95, as per "length(Model4)". However, I would like to create a new
> Model4 (let's say "NewModel4"), in which it has a length of 90,
> instead of 95. Is there a way to do this? Thanks, and any assistance
> would be greatly appreciated! [[alternative HTML version deleted]]

This is a plain text mailing list.  Please *DO NOT* post in HTML.
(In general this scrambles your post and makes it incomprehensible.)

To get to your question:  What you ask makes little sense.  The "length"
of a matrix is the total number of entries --- nrow(<matrix>) *
ncol(<matrix>).  Changing the "length" of a matrix would either involve
changing the number of rows or the number of columns (or both).

Why do you want to do this?  What are you trying to accomplish?
What does dim(Model4) produce?

Don't you think it's time you got serious and learned a bit about R?
(There are many excellent introductory documents available online.)

cheers,

Rolf Turner

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Rolf Turner

On 31/03/19 10:22 AM, [hidden email] wrote:

> Hi Rolf,
>
> My apologies - I meant "layers" as opposed to "length". The goal is to
> reduce the number of layers to 90 (from 95).
>
> dim (Model4) yields:
>
> 64   128   95
>
>
> You can see the 95 there. That is what I would like to reduce to 90, or
> isolate layer 1 to layer 90.

Please keep the list in the set of recipients.  I am not your private
consultant, and furthermore others on the list may be able to provide
better advice than I.  I have CC-ed this message to the list.

To keep only "layers" 1 through 90 you could do:

     Model4.chopped <- Model4[,,1:90]

As I said before, it really is time that you learned something about R
(e.g. by studying a tutorial).

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

R-sig-geo mailing list
Hi Rolf (and others),

I tried your suggestion, but when I used dim(Model4.chopped), it still shows 95 layers, as shown below:
8192    95 
I also find that the total number of cells is rather low for that many layers. I started with 778240 cells over 95 layers. 
-----Original Message-----
From: Rolf Turner <[hidden email]>
To: rain1290 <[hidden email]>
Cc: [hidden email] <[hidden email]>
Sent: Sat, Mar 30, 2019 5:57 pm
Subject: Re: [FORGED] [R-sig-Geo] Modifying the length of a matrix variable


On 31/03/19 10:22 AM, [hidden email] wrote:

> Hi Rolf,
>
> My apologies - I meant "layers" as opposed to "length". The goal is to
> reduce the number of layers to 90 (from 95).
>
> dim (Model4) yields:
>
> 64   128   95
>
>
> You can see the 95 there. That is what I would like to reduce to 90, or
> isolate layer 1 to layer 90.

Please keep the list in the set of recipients.  I am not your private
consultant, and furthermore others on the list may be able to provide
better advice than I.  I have CC-ed this message to the list.

To keep only "layers" 1 through 90 you could do:

    Model4.chopped <- Model4[,,1:90]

As I said before, it really is time that you learned something about R
(e.g. by studying a tutorial).

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Rolf Turner

On 31/03/19 11:14 AM, [hidden email] wrote:

> Hi Rolf (and others),
>
> I tried your suggestion, but when I used dim(Model4.chopped), it still
> shows 95 layers, as shown below:
>
> 8192    95
>
> I also find that the total number of cells is rather low for that many
> layers. I started with 778240 cells over 95 layers.

Well then you're doing something wrong, or there is something that you
haven't told us.

E.g.:
> junk <- array(runif(64*128*95),dim=c(64,128,95))
> junk.chopped <- junk[,,1:90]
> dim(junk)
> [1]  64 128  95
> dim(junk.chopped)
> [1]  64 128  90

Perhaps Model.4 has some structure other than that of an array.
(Originally you said it was a matrix.)

You really need to get your terminology and ideas *clear* in order to
have any hope of receiving useful advice.

I have no idea what you are on about in respect of "the number of
cells".  My mind-reading machine is in the repair shop.  I strongly
suspect that your thoughts are confused.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

R-sig-geo mailing list
Yes, I reproduced the example above and it works just fine (and is what I want!!), but I cannot see why it does not work with my data, as it is a 3-dimensional array (latitude, longitude and time). 
This is what comes from print(Model4):
3 variables (excluding dimension variables):
        double onedaymax[lon,lat,time]   (Contiguous storage)  
            units: mm/day
        double fivedaymax[lon,lat,time]   (Contiguous storage)  
            units: mm/day
        short Year[time]   (Contiguous storage)  

     3 dimensions:
        time  Size:95
        lat  Size:64
            units: degree North
        lon  Size:128
            units: degree East
I reviewed it over and over again, but I cannot see why this would not work?
Thanks, 


-----Original Message-----
From: Rolf Turner <[hidden email]>
To: rain1290 <[hidden email]>
Cc: r-sig-geo <[hidden email]>
Sent: Sat, Mar 30, 2019 6:49 pm
Subject: Re: [FORGED] [R-sig-Geo] Modifying the length of a matrix variable


On 31/03/19 11:14 AM, [hidden email] wrote:

> Hi Rolf (and others),
>
> I tried your suggestion, but when I used dim(Model4.chopped), it still
> shows 95 layers, as shown below:
>
> 8192    95
>
> I also find that the total number of cells is rather low for that many
> layers. I started with 778240 cells over 95 layers.

Well then you're doing something wrong, or there is something that you
haven't told us.

E.g.:
> junk <- array(runif(64*128*95),dim=c(64,128,95))
> junk.chopped <- junk[,,1:90]
> dim(junk)
> [1]  64 128  95
> dim(junk.chopped)
> [1]  64 128  90

Perhaps Model.4 has some structure other than that of an array.
(Originally you said it was a matrix.)

You really need to get your terminology and ideas *clear* in order to
have any hope of receiving useful advice.

I have no idea what you are on about in respect of "the number of
cells".  My mind-reading machine is in the repair shop.  I strongly
suspect that your thoughts are confused.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Rolf Turner

On 31/03/19 12:30 PM, [hidden email] wrote:

> Yes, I reproduced the example above and it works just fine (and is what
> I want!!), but I cannot see why it does not work with my data, as it is
> a 3-dimensional array (latitude, longitude and time).
>
> This is what comes from print(Model4):
>
> 3 variables (excluding dimension variables): double
> onedaymax[lon,lat,time] (Contiguous storage) units: mm/day double
> fivedaymax[lon,lat,time] (Contiguous storage) units: mm/day short
> Year[time] (Contiguous storage) 3 dimensions: time Size:95 lat Size:64
> units: degree North lon Size:128 units: degree East
>
>
> I reviewed it over and over again, but I cannot see why this would not work?

Psigh!  Clearly Model4 is *not* an array!!!  It is an object of some
"specialised" class (for which there is specialised print() method).  I
have no idea what that class might be, but *you can tell.  What does

    class(Model4)

return?

Where did this "Model4" object come from?  What are you trying to *do*?

You might be able to get somewhere by searching (e.g. via Google) on
"subsetting objects of class melvin" where "melvin" is what is returned
by "class(Model4)".

Doing

     str(Model4)

could be enlightening (but given your stubborn refusal to acquire
insight into the workings of R, I am not optimistic).

This is not magic or religion.  You need to *understand* what you are
dealing with, and proceed rationally.  Don't just hammer and hope.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

R-sig-geo mailing list
In reply to this post by R-sig-geo mailing list
Model4 <- brick("MaxPrecCCCMACanESM2rcp45.nc", var="onedaymax")
That is how Model4 is derived. 
When trying class(Model4), I receive:
[1] "RasterBrick"
attr(,"package")
[1] "raster"

Meanwhile, I will check on Google to see what I come up with in terms of your suggestion. :)
-----Original Message-----
From: Rolf Turner <[hidden email]>
To: rain1290 <[hidden email]>
Cc: r-sig-geo <[hidden email]>
Sent: Sat, Mar 30, 2019 7:43 pm
Subject: Re: [FORGED] [R-sig-Geo] Modifying the length of a matrix variable


On 31/03/19 12:30 PM, [hidden email] wrote:

> Yes, I reproduced the example above and it works just fine (and is what
> I want!!), but I cannot see why it does not work with my data, as it is
> a 3-dimensional array (latitude, longitude and time).
>
> This is what comes from print(Model4):
>
> 3 variables (excluding dimension variables): double
> onedaymax[lon,lat,time] (Contiguous storage) units: mm/day double
> fivedaymax[lon,lat,time] (Contiguous storage) units: mm/day short
> Year[time] (Contiguous storage) 3 dimensions: time Size:95 lat Size:64
> units: degree North lon Size:128 units: degree East
>
>
> I reviewed it over and over again, but I cannot see why this would not work?

Psigh!  Clearly Model4 is *not* an array!!!  It is an object of some
"specialised" class (for which there is specialised print() method).  I
have no idea what that class might be, but *you can tell.  What does

    class(Model4)

return?

Where did this "Model4" object come from?  What are you trying to *do*?

You might be able to get somewhere by searching (e.g. via Google) on
"subsetting objects of class melvin" where "melvin" is what is returned
by "class(Model4)".

Doing

    str(Model4)

could be enlightening (but given your stubborn refusal to acquire
insight into the workings of R, I am not optimistic).

This is not magic or religion.  You need to *understand* what you are
dealing with, and proceed rationally.  Don't just hammer and hope.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Rolf Turner

On 31/03/19 12:56 PM, [hidden email] wrote:

> Model4 <- brick("MaxPrecCCCMACanESM2rcp45.nc", var="onedaymax")
>
>
> That is how Model4 is derived.
>
> When trying class(Model4), I receive:
>
> [1] "RasterBrick" attr(,"package") [1] "raster"
>
> **//___^
>
> Meanwhile, I will check on Google to see what I come up with in terms of
> your suggestion. :)

I know nothing about rasters, the brick() function, or the raster
package, so include me out at this stage.

Others on the list may be able to help you.  Particular if you can force
yourself to ask a *focussed* question.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Ben Tupper
In reply to this post by R-sig-geo mailing list
Hi,

It looks like you are accessing a NetCDF file or it's kin.  Like Rolf, I'm challenged to understand from your description what you are after.  Assuming you do indeed have a NetCDF file and are using the ncdf4 package, you can retrieve the first 90 layers as shown below using the varid, start and count arguments to ncvar_get().  Obviously, this is untested since your data isn't available to us.

library(ncdf4)
filename = "/path/to/foo.nc"
ncObject <- nc_open(filename)
m <- ncvar_get(ncObject varid = "onedaymax", start = c(1,1,1), count = c(-1,-1, 90))
nc_close(ncObject)


See more details and examples here... https://www.rdocumentation.org/packages/ncdf4/versions/1.16.1/topics/ncvar_get


You might also consider using the raster package which will retrieve multilayer georeferenced rasters for you from the same NetCDF file.

library(raster)
B <- brick(filename, varname = "onedaymax")
B <- B[[-c(91:95)]]    # <- drop the last layers 91, 92, ...95

See the docs here... https://www.rdocumentation.org/packages/raster/versions/2.8-19/topics/brick

Cheers,
Ben

> On Mar 30, 2019, at 7:30 PM, rain1290--- via R-sig-Geo <[hidden email]> wrote:
>
> Yes, I reproduced the example above and it works just fine (and is what I want!!), but I cannot see why it does not work with my data, as it is a 3-dimensional array (latitude, longitude and time).
> This is what comes from print(Model4):
> 3 variables (excluding dimension variables):
>        double onedaymax[lon,lat,time]   (Contiguous storage)  
>            units: mm/day
>        double fivedaymax[lon,lat,time]   (Contiguous storage)  
>            units: mm/day
>        short Year[time]   (Contiguous storage)  
>
>     3 dimensions:
>        time  Size:95
>        lat  Size:64
>            units: degree North
>        lon  Size:128
>            units: degree East
> I reviewed it over and over again, but I cannot see why this would not work?
> Thanks,
>
>
> -----Original Message-----
> From: Rolf Turner <[hidden email]>
> To: rain1290 <[hidden email]>
> Cc: r-sig-geo <[hidden email]>
> Sent: Sat, Mar 30, 2019 6:49 pm
> Subject: Re: [FORGED] [R-sig-Geo] Modifying the length of a matrix variable
>
>
> On 31/03/19 11:14 AM, [hidden email] wrote:
>
>> Hi Rolf (and others),
>>
>> I tried your suggestion, but when I used dim(Model4.chopped), it still
>> shows 95 layers, as shown below:
>>
>> 8192    95
>>
>> I also find that the total number of cells is rather low for that many
>> layers. I started with 778240 cells over 95 layers.
>
> Well then you're doing something wrong, or there is something that you
> haven't told us.
>
> E.g.:
>> junk <- array(runif(64*128*95),dim=c(64,128,95))
>> junk.chopped <- junk[,,1:90]
>> dim(junk)
>> [1]  64 128  95
>> dim(junk.chopped)
>> [1]  64 128  90
>
> Perhaps Model.4 has some structure other than that of an array.
> (Originally you said it was a matrix.)
>
> You really need to get your terminology and ideas *clear* in order to
> have any hope of receiving useful advice.
>
> I have no idea what you are on about in respect of "the number of
> cells".  My mind-reading machine is in the repair shop.  I strongly
> suspect that your thoughts are confused.
>
> cheers,
>
> Rolf
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> 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/

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

Stephen Stewart
In reply to this post by Rolf Turner
The last few messages provided important information which was previously
absent. Try:

subset = Model4[[1:90]]

This will subset the brick to layers 1 through 90.

I would also suggest some further reading around the raster package and
NetCDF files (e.g. the ncdf4 package) would be useful to you.

On Sun, 31 Mar 2019 at 11:06, Rolf Turner <[hidden email]> wrote:

>
> On 31/03/19 12:56 PM, [hidden email] wrote:
>
> > Model4 <- brick("MaxPrecCCCMACanESM2rcp45.nc", var="onedaymax")
> >
> >
> > That is how Model4 is derived.
> >
> > When trying class(Model4), I receive:
> >
> > [1] "RasterBrick" attr(,"package") [1] "raster"
> >
> > **//___^
> >
> > Meanwhile, I will check on Google to see what I come up with in terms of
> > your suggestion. :)
>
> I know nothing about rasters, the brick() function, or the raster
> package, so include me out at this stage.
>
> Others on the list may be able to help you.  Particular if you can force
> yourself to ask a *focussed* question.
>
> cheers,
>
> Rolf
>
> --
> Honorary Research Fellow
> Department of Statistics
> University of Auckland
> Phone: +64-9-373-7599 ext. 88276
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Stephen Stewart

PhD Student
School of Ecosystem and Forest Sciences
Faculty of Science
University of Melbourne
Reply | Threaded
Open this post in threaded view
|

Re: [FORGED] Modifying the length of a matrix variable

R-sig-geo mailing list
In reply to this post by R-sig-geo mailing list
SUCCESS! Thank you so, so much, Stephen! Indeed, that worked out well, and dim(subset) shows:
64   128    90
I am very, very grateful for this! Thanks to the others, too, for their assistance with this!

-----Original Message-----
From: Stephen Stewart <[hidden email]>
To: Rolf Turner <[hidden email]>
Cc: rain1290 <[hidden email]>; R-sig-geo Mailing List <[hidden email]>
Sent: Sat, Mar 30, 2019 8:16 pm
Subject: Re: [R-sig-Geo] [FORGED] Modifying the length of a matrix variable

The last few messages provided important information which was previously absent. Try:

subset = Model4[[1:90]]

This will subset the brick to layers 1 through 90.

I would also suggest some further reading around the raster package and NetCDF files (e.g. the ncdf4 package) would be useful to you.

On Sun, 31 Mar 2019 at 11:06, Rolf Turner <[hidden email]> wrote:


On 31/03/19 12:56 PM, [hidden email] wrote:

> Model4 <- brick("MaxPrecCCCMACanESM2rcp45.nc", var="onedaymax")
>
>
> That is how Model4 is derived.
>
> When trying class(Model4), I receive:
>
> [1] "RasterBrick" attr(,"package") [1] "raster"
>
> **//___^
>
> Meanwhile, I will check on Google to see what I come up with in terms of
> your suggestion. :)

I know nothing about rasters, the brick() function, or the raster
package, so include me out at this stage.

Others on the list may be able to help you.  Particular if you can force
yourself to ask a *focussed* question.

cheers,

Rolf

--
Honorary Research Fellow
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo