R Under development (unstable) (2023-08-09 r84924 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # examples with the sf::nc polygon dataset NOT ABOUT NETCDF's, see e.g. stars.R netcdf.R > suppressPackageStartupMessages(library(sf)) > nc = read_sf(system.file("gpkg/nc.gpkg", package="sf")) > m = st_set_geometry(nc, NULL) > n = as.matrix(m[c("BIR74", "SID74", "NWBIR74", "BIR79", "SID79", "NWBIR79")]) > dim(n) = c(county = 100, var = 3, year = 2) > dimnames(n) = list(county = nc$NAME, var = c("BIR", "SID", "NWBIR"), year = c(1974, 1979)) > suppressPackageStartupMessages(library(stars)) > (st = st_as_stars(pop = n)) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 8 538 1657.032 1784 30757 dimension(s): from to values county 1 100 Ashe,...,Brunswick var 1 3 BIR , SID , NWBIR year 1 2 1974, 1979 > foo <- st %>% st_set_dimensions(1, st_geometry(nc)) # %>% st_set_dimensions(3, c(1974, 1979)) > st %>% st_set_dimensions(1, st_geometry(nc)) %>% st_set_dimensions(names = c("geometries", "var", "year")) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 8 538 1657.032 1784 30757 dimension(s): from to refsys point geometries 1 100 NAD27 FALSE var 1 3 NA NA year 1 2 NA NA values geometries MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... var BIR , SID , NWBIR year 1974, 1979 > foo stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 8 538 1657.032 1784 30757 dimension(s): from to refsys point county 1 100 NAD27 FALSE var 1 3 NA NA year 1 2 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... var BIR , SID , NWBIR year 1974, 1979 > st_bbox(foo) xmin ymin xmax ymax -84.32385 33.88199 -75.45698 36.58965 > (x = st_as_sf(foo)) Simple feature collection with 100 features and 6 fields Geometry type: MULTIPOLYGON Dimension: XY Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965 Geodetic CRS: NAD27 First 10 features: BIR.1974 SID.1974 NWBIR.1974 BIR.1979 SID.1979 NWBIR.1979 1 1091 1 10 1364 0 19 2 487 0 10 542 3 12 3 3188 5 208 3616 6 260 4 508 1 123 830 2 145 5 1421 9 1066 1606 3 1197 6 1452 7 954 1838 5 1237 7 286 0 115 350 2 139 8 420 0 254 594 2 371 9 968 4 748 1190 2 844 10 1612 1 160 2038 5 176 county 1 MULTIPOLYGON (((-81.47276 3... 2 MULTIPOLYGON (((-81.23989 3... 3 MULTIPOLYGON (((-80.45634 3... 4 MULTIPOLYGON (((-76.00897 3... 5 MULTIPOLYGON (((-77.21767 3... 6 MULTIPOLYGON (((-76.74506 3... 7 MULTIPOLYGON (((-76.00897 3... 8 MULTIPOLYGON (((-76.56251 3... 9 MULTIPOLYGON (((-78.30876 3... 10 MULTIPOLYGON (((-80.02567 3... > frac = function(x) x[2] / x[1] > frac2 = function(x) c(sidsr = x[2] / x[1], nwbr = x[3] / x[1]) > frac2an = function(x) c(x[2] / x[1], x[3] / x[1]) > st_apply(foo, c(1,3), frac) stars object with 2 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. frac 0 0.001197874 0.001930504 0.002042241 0.002599348 0.00955414 dimension(s): from to refsys point county 1 100 NAD27 FALSE year 1 2 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... year 1974, 1979 > st_apply(foo, c(1,3), frac2) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 0.001923331 0.005119673 0.1567576 0.3065298 0.7727273 dimension(s): from to refsys point frac2 1 2 NA NA county 1 100 NAD27 FALSE year 1 2 NA NA values frac2 sidsr, nwbr county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... year 1974, 1979 > st_apply(foo, c(1,3), frac2an) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 0.001923331 0.005119673 0.1567576 0.3065298 0.7727273 dimension(s): from to refsys point frac2an 1 2 NA NA county 1 100 NAD27 FALSE year 1 2 NA NA values frac2an NULL county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... year 1974, 1979 > library(abind) > (x = aperm(st_apply(foo, c(1,3), frac2), c(2,3,1))) stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 0.001923331 0.005119673 0.1567576 0.3065298 0.7727273 dimension(s): from to refsys point county 1 100 NAD27 FALSE year 1 2 NA NA frac2 1 2 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... year 1974, 1979 frac2 sidsr, nwbr > y = aperm(st_apply(foo, c(1,3), frac2), c("county","year","frac2")) > all.equal(st_dimensions(x), st_dimensions(y)) [1] TRUE > > split(foo, 2) stars object with 2 dimensions and 3 attributes attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. BIR 248 1176.75 2265 3761.770 4450.75 30757 SID 0 2.00 5 7.515 9.00 57 NWBIR 1 206.00 742 1201.810 1315.75 11631 dimension(s): from to refsys point county 1 100 NAD27 FALSE year 1 2 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... year 1974, 1979 > split(foo, 3) stars object with 2 dimensions and 2 attributes attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. 1974 0 8 501.5 1452.367 1580.50 21588 1979 0 9 614.0 1861.697 2040.25 30757 dimension(s): from to refsys point county 1 100 NAD27 FALSE var 1 3 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... var BIR , SID , NWBIR > > st_crs(foo) Coordinate Reference System: User input: NAD27 wkt: GEOGCRS["NAD27", DATUM["North American Datum 1927", ELLIPSOID["Clarke 1866",6378206.4,294.978698213898, LENGTHUNIT["metre",1]]], PRIMEM["Greenwich",0, ANGLEUNIT["degree",0.0174532925199433]], CS[ellipsoidal,2], AXIS["geodetic latitude (Lat)",north, ORDER[1], ANGLEUNIT["degree",0.0174532925199433]], AXIS["geodetic longitude (Lon)",east, ORDER[2], ANGLEUNIT["degree",0.0174532925199433]], USAGE[ SCOPE["Geodesy."], AREA["North and central America: Antigua and Barbuda - onshore. Bahamas - onshore plus offshore over internal continental shelf only. Belize - onshore. British Virgin Islands - onshore. Canada onshore - Alberta, British Columbia, Manitoba, New Brunswick, Newfoundland and Labrador, Northwest Territories, Nova Scotia, Nunavut, Ontario, Prince Edward Island, Quebec, Saskatchewan and Yukon - plus offshore east coast. Cuba - onshore and offshore. El Salvador - onshore. Guatemala - onshore. Honduras - onshore. Panama - onshore. Puerto Rico - onshore. Mexico - onshore plus offshore east coast. Nicaragua - onshore. United States (USA) onshore and offshore - Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut, Delaware, Florida, Georgia, Idaho, Illinois, Indiana, Iowa, Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts, Michigan, Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire, New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio, Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota, Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia, Wisconsin and Wyoming - plus offshore . US Virgin Islands - onshore."], BBOX[7.15,167.65,83.17,-47.74]], ID["EPSG",4267]] > plot(foo) > > # subset vector cube: > foo[nc[1]] stars object with 3 dimensions and 1 attribute attribute(s): Min. 1st Qu. Median Mean 3rd Qu. Max. pop 0 8 538 1657.032 1784 30757 dimension(s): from to refsys point county 1 100 NAD27 FALSE var 1 3 NA NA year 1 2 NA NA values county MULTIPOLYGON (((-81.47276...,...,MULTIPOLYGON (((-78.65572... var BIR , SID , NWBIR year 1974, 1979 > > proc.time() user system elapsed 1.21 0.25 1.45