Inspecting R Objects

jbedia edited this page Sep 9, 2014 · 7 revisions

Inspecting S4 Objects

Aim of the practice

Learn the basics of operating with S4 objects created by commonly used R packages, such as sp.


  • This practice assumes you have a working installation of the ecomsUDG.Raccess package (see intallation instructions)
  • We will also use the sp package, available in the CRAN repositories. It can be installed within an R console with administrative permissions with: install.packages('sp')

Practice Steps

Loading example S4 object

In this practice, we will use an example forecast of daily mean surface temperature for July 2006 considering the first member of the CFSv2 hindcast. This data can be adquired inside a R session by requesting it to the ECOMS-UDG portal:

loginECOMS_UDG('myusername', 'mypassword')
ref <- loadECOMS(dataset = "CFSv2_seasonal_16", var = "tas", members = 1, lonLim = c(-10,-1), latLim = c(36,40), season = 7, years = 2006, leadMonth = 0, time = "DD")

When the command has finished downloading all the data, we can plot the mean field, which we will convert afterwards to an spatial object.

# This is the spatial mean of the reference field
ref.field <- apply(ref$Data, MARGIN = c(3,2), FUN = mean, na.rm = TRUE)

Now we can convert the mean field to an sp object using the grid coordinates:

xy <- expand.grid(ref$xyCoords$x, ref$xyCoords$y)
# This step ensures regularity of the CFS grid, which is not perfectly regular:
xy.coords <- coordinates(points2grid(points = SpatialPoints(xy), tolerance = .003))	
df <-, as.vector(t(ref.field)))
names(df) <- c("x","y","ref.field")
coordinates(df) <- c(1,2)
gridded(df) <- TRUE

At this we can check we have actually an sp object with the class command: class(df) will show:

[1] "SpatialPixelsDataFrame"
[1] "sp"

Our object is also an S4 Object:

> isS4(df)
[1] TRUE

Our variable df contains a S4 object of the class SpatialPixelsDataFrame, defined in the package sp. This can seem a little confusing as we had a regular data.frame S3 object, but the sp package converted the data.frame to SpatialPointsDataFrame for us when we invoked the coordinates command. In normal conditions, when we haven't an auxiliary command that creates the object for us, we have to create it by calling to the constructor, with the new command.

Inspecting object slots

An R object is a data structure that contains a description (represented by its class name), a named set of variables (slots), and can provide custom logic for generic functions (methods).

We can check the data slots available in an object instance by calling to the slotNames command: slotNames(df):

[1] "data"        "coords.nrs"  "grid"        "grid.index"  "coords"     
[6] "bbox"        "proj4string"

To access an slot content, we use the @ operator. For example, we can check the dimensions of the data and coords slots:

> dim(df@data)
[1] 55  1
> dim(df@coords)
[1] 55  2
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.