我正在使用RGeostats包来计算变异函数,并希望从RGeotats类对象中提取元素。示例如下:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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')
现在RGeostats中的绘图功能还不太好(或者我打算在程序的帮助文件中找到如何更好地控制输出的信息),所以我决定提取一些字段,以便可以在基准R中绘制图表。vario.calc创建的对象是RGeostats创建的“类”对象,其结构如下:
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 ...
我想将'@'对象'sw','hh'和'gg'提取到数据框中。我可以通过执行以下操作来实现:
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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)
在这里,我使用'slot'函数从类对象中提取列表,然后将所需的子列表子集化。但是,我很想知道如何为这样的类列表更直接地访问(指向)这些子列表,而不是提取列表然后对列表进行子设置。
另外,虽然语法v [[1]] [1]允许我访问'sw','hh'和'gg'子列表-我无法计算出语法访问其他项,例如'toldis'或“名称”变量-我对此处的列表结构一无所知
答案 0 :(得分:0)
在实现S4对象可以包含另一个作为列表项嵌入的S4对象之后,我发现了以下解决方案。要直接访问,请说语法的'vardirs'插槽的'sw'插槽为:
Vg.Zn@vardirs[[1]]@sw