1

I am trying to work with Columbia's 3-dimensional gridded population of the world data, v4 (Gridded Population of the World (GPW), v4). it has population estimates in a 1x1 grid for seq(2000,2020,5). https://sedac.ciesin.columbia.edu/data/collection/gpw-v4 . complicating the fact is that there are 5 years and 10 variables, vars <- c( "ccode", "datacode", "year", "admlevel", "sexlevel", "agelevel", "gyear0", "gyearT", "glevel", "ycensus" ) indicated in the textual description, but I can worry about these later. right now, I just want to get some variable(s) in one particular year.

library(ncdf4)
nc= nc_ncopen("gpw_v4_population_count_rev11_1_deg.nc")
print(nc)

tells me

     1 variables (excluding dimension variables):
        float Population Count, v4.11 (2000, 2005, 2010, 2015, 2020): 1 degree[longitude,latitude,raster]   
      ... more info ...
     3 dimensions:
        longitude  Size:360 
            units: degrees_east
            long_name: longitude
        latitude  Size:180 
            units: degrees_north
            long_name: latitude
        raster  Size:20   *** is unlimited *** 
            units: unknown
            long_name: raster

I am about to do a victory dance with mydf2000 <- as.data.frame( nc[,,1] ) but my hopes were cruelly dashed.

I got a little further with

library(raster)
library(ncdf4)

fnm <- "gpw_v4_population_count_rev11_1_deg.nc"
ncbrick <- brick(fnm)
dim(ncbrick) # 180, 360, 20
dim(ncbrick[,,1] ## 64800, 20 <-- huh?  I wanted 180, 360

could someone please let me know how to read the x-th dimensional 180x360 grid into a data frame?

1 Answer 1

1

Use theterra package:

library(terra)

Read using rast:

d = rast("./gpw_v4_population_count_rev11_1_deg.nc")

Get layers with [[index]]:

dim(d[[1]])
### [1] 180 360   1

Convert to a normal matrix with as.matrix and wide=TRUE:

dim(as.matrix(d[[1]],wide=TRUE))
### [1] 180 360

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.