Markm0705
Markm0705

Reputation: 1440

Extracting elements from an R class object

I'm using the RGeostats package to compute variograms and would like to extract the elements from an RGeotats class object. Example as follows:

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load demo pollution data
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fn <- 'http://rgeostats.free.fr/doc/Files/Pollution.dat'
dt <- read.table(fn, header = F, na.strings = 'NA')
names(dt) <- c('ID','X','Y','Pb','Zn')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load RGeostats package
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Not on CRAN download zip file from 
# http://rgeostats.free.fr/download.php
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require(RGeostats)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Load data into a Rgeostats database file
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
db <- db.create(dt, flag.grid = F, ndim = 2, autoname = F)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Define fields to be used as coordinates and variable
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
db <- db.locate(db,'X','x',1)
db <- db.locate(db,'Y','x',2)
db <- db.locate(db,'Zn','z',1)

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Plot a data map using RGeostats 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
plot(db, pch = 21, bg = 'red', col = 'black',title = 'Zn')

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Compute omnidirectional semivariogram for Zn
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vg.Zn <- vario.calc(db, lag = 1, nlag = 10, calcul = 'vg')
plot(Zn.Vr, npairw = TRUE, npairpt = 1, title = 'Zn Omni')

Now the plotting functionality in RGeostats in not that great (or I'm to find in the program's help file how to better control the output) so I decided to extract some of the fields so I can plot the chart in base R. The object created by vario.calc is a 'class' object created by RGeostats that has the following structure:

 str(Vg.Zn)
Formal class 'vario' [package "RGeostats"] with 10 slots
  ..@ calcul   : chr "vg"
  ..@ by.sample: logi FALSE
  ..@ ndim     : int 2
  ..@ nvar     : int 1
  ..@ scale    : num 1
  ..@ means    : num(0) 
  ..@ dates    : num [1:2] -1e+30 1e+30
  ..@ vars     : num 12.9
  ..@ names    : chr "Zn"
  ..@ vardirs  :List of 1
  .. ..$ :Formal class 'vardir' [package "RGeostats"] with 17 slots
  .. .. .. ..@ npas        : num 10
  .. .. .. ..@ npatot      : int 10
  .. .. .. ..@ opt.code    : num 0
  .. .. .. ..@ idate       : int 1
  .. .. .. ..@ pas         : num 1
  .. .. .. ..@ toldis      : num 0.5
  .. .. .. ..@ tolang      : num 90
  .. .. .. ..@ bench       : num 0
  .. .. .. ..@ cylrad      : num 0
  .. .. .. ..@ tolcode     : num 0
  .. .. .. ..@ flag.regular: num 1
  .. .. .. ..@ breaks      : num 0
  .. .. .. ..@ codir       : num [1:2] 1 0
  .. .. .. ..@ size        : int 10
  .. .. .. ..@ sw          : num [1:10] 3 127 187 209 234 233 202 194 218 198
  .. .. .. ..@ hh          : num [1:10] 0.389 1.079 2.039 3.004 4.011 ...
  .. .. .. ..@ gg          : num [1:10] 0.462 9.213 4.667 7.32 5.729 ...

I would like to extract the '@' objects 'sw', 'hh' and 'gg' to a dataframe. This I can achieve by doing the following:

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Extract variogram information from this class
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
v <- slot(Vg.Zn,'vardirs')
p <- data.frame(unlist(v[[1]][1][1]))
h <- data.frame(unlist(v[[1]][1][2]))
g <- data.frame(unlist(v[[1]][1][2]))

Vg.Zn2 <- cbind(p,h)
Vg.Zn2 <- cbind(Vg.Zn2,g)

Here I use the 'slot' function to extract the list from the class object then subset out the sub-lists desired. However, I would be interested to know how to access (point to) these sub-list more directly for such a class list, rather than extracting the list then sub-setting the list.

Additionally while the syntax v[[1]][1] gave me access to the 'sw', 'hh' and 'gg' sublist - I could not work out the syntax access the other items such as 'toldis' or 'names' variables - I'm not understanding something about the list structure here

Upvotes: 0

Views: 2539

Answers (1)

Markm0705
Markm0705

Reputation: 1440

I found the solution as follows after realising the S4 object can contain another S4 object embedded as a list item. To directly access say the 'sw' slot of the 'vardirs' slot of the syntax is:

Vg.Zn@vardirs[[1]]@sw

Upvotes: 1

Related Questions