矩阵列出与绘图功能不兼容

时间:2012-04-13 15:14:29

标签: r list for-loop matrix

我有一个以下格式的列表对象:

> str(TEST.Lmm)
List of 16
 $ NofSimulations: num 2
 $ r             : num [1:511] 0.851 1.276 1.702 2.127 2.553 ...
 $ Ltheo         : num [1:511] 10.7 11.4 14.2 23.7 28.3 ...
 $ Lmm           : num [1:511] 27.6 27.6 29.6 58.2 74.5 ...
 $ Lmm.sims      : num [1:511, 1:2] 0 0 14.5 35.5 35.5 ...
 $ Lmm.maxdev    : num [1:3] 266.5 45 25.4
 $ Lmm.intdev2   : num [1:3] 7563233 333376 53004
 $ Lmm.intdev1   : num [1:3] 46093 11597 4472
 $ lower         : num [1:511] 0 0 0 19.3 19.7 ...
 $ upper         : num [1:511] 0 0 14.5 35.5 35.5 ...
 $ k             : num 1
 $ pmaxdev       : num 0.333
 $ pintdev2      : num 0.333
 $ pintdev1      : num 0.333
 $ typeIerror    : num 1
 $ call          : language mtests.Lmm(NofSimulations = 2, k = 1, data.mpp = Ahx[[7]], permutate = TRUE,      rmin = rmin, rmax = rmax)
 - attr(*, "class")= chr "mtests.Lmm"

我可以使用以下绘图命令[我正在使用尚未在CRAN上发布的未来R包的功能]来绘制这些结果而没有任何问题。

plot.mtests.Lmm(TEST.Lmm, Plot.zeroline=TRUE, main="", xlim=c(0,rmax), ylim=c(-500,500), xlab=expression(italic(r)), ylab=expression(italic( hat(L)[mm](r)-hat(L)(r))), las=1) #this works fine!

绘图函数相当复杂,但重要的是要知道它使用上面列表中的大部分元素(例如$ NofSimulations,$ r等)

我进行了一系列分析,结果是以下格式的矩阵:

> Greenland.Lmm
               [,1]         [,2]         [,3]         [,4]         [,5]         [,6]        
NofSimulations 2            2            2            2            2            2           
r              Numeric,511  Numeric,512  Numeric,512  Numeric,512  Numeric,512  Numeric,511 
Ltheo          Numeric,511  Numeric,512  Numeric,512  Numeric,512  Numeric,512  Numeric,511 
Lmm            Numeric,511  Numeric,512  Numeric,512  Numeric,512  Numeric,512  Numeric,511 
Lmm.sims       Numeric,1022 Numeric,1024 Numeric,1024 Numeric,1024 Numeric,1024 Numeric,1022
Lmm.maxdev     Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3   
Lmm.intdev2    Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3   
Lmm.intdev1    Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3    Numeric,3   
lower          Numeric,511  Numeric,512  Numeric,512  Numeric,512  Numeric,512  Numeric,511 
upper          Numeric,511  Numeric,512  Numeric,512  Numeric,512  Numeric,512  Numeric,511 
k              1            1            1            1            1            1           
pmaxdev        0.3333333    0.3333333    0.3333333    0.3333333    0.3333333    0.6666667   
pintdev2       0.3333333    0.3333333    0.3333333    0.3333333    0.3333333    1           
pintdev1       0.3333333    0.3333333    0.3333333    0.3333333    0.3333333    0.6666667   
typeIerror     1            1            1            1            1            1           
call           Expression   Expression   Expression   Expression   Expression   Expression 

我想使用前面介绍的plot命令分别绘制每个。但是,当我执行以下操作时,收到错误消息:

# step 1) transform matrix into list
xnew<-lapply(seq_len(ncol(Greenland.Lmm[,1,drop=FALSE])), function(i) Greenland.Lmm[,1,drop=FALSE][,i])

> plot.mtests.Lmm(xnew, Plot.zeroline=TRUE, main="", xlim=c(0,max(Greenland.Lmm[,1]$r)), ylim=c(-500,500), xlab=expression(italic(r)), ylab=expression(italic( hat(L)[mm](r)-hat(L)(r))), las=1)
Plot of r vs L[mm](r)-L(r).
Error in 1:x$NofSimulations : argument of length 0

问题是我无法访问plot.mtests.Lmm函数调用的列表中的数据。

> xnew$NofSimulations
NULL
#vs
> xnew[[1]]$NofSimulations
[1] 2

所以我的选择是修改plot.mtests.Lmm函数(我不想这样做)或找到解决它的方法......但是如何?我怎样才能获得额外的[[1]]?另外,我希望把所有这些都放到一个漂亮的小代码中(也许是for循环?),这样我就不必手动复制每个步骤了。我将不胜感激任何最佳方法的想法!

2 个答案:

答案 0 :(得分:1)

以下内容应该让xnew$NofSimulations在您给出的示例中返回2(也就是说,只查看Greenland.Lmm的第一列)。

xnew<-unlist(lapply(seq_len(ncol(Greenland.Lmm[,1,drop=FALSE])),
  function(i) Greenland.Lmm[,1,drop=FALSE][,i]), recursive = FALSE)

然而,这个例子看起来很像你想做的事情。如果您计划按顺序绘制Greenland.Lmm每列中的数据,我建议您创建xnew,就像您在原始问题中所做的那样

xnew<-lapply(seq_len(ncol(Greenland.Lmm)), function(i) Greenland.Lmm[,i])

并执行以下操作:

plot.mtests.Lmm(xnew[[1]], Plot.zeroline=TRUE, main="", 
  xlim=c(0,max(xnew[[1]]$r)), ylim=c(-500,500), 
  xlab=expression(italic(r)), 
  ylab=expression(italic( hat(L)[mm](r)-hat(L)(r))), las=1)

您可以用xnew列表索引替换您想要的任何列号。

如果要一举绘制每列中的数据:

lapply(seq_len(ncol(Greenland.Lmm)),function(x){
  xnew<-Greenland.Lmm[,x]
  plot.mtests.Lmm(xnew, Plot.zeroline=TRUE, main="",
    xlim=c(0,max(xnew$r)), 
    ylim=c(-500,500), xlab=expression(italic(r)), 
    ylab=expression(italic( hat(L)[mm](r)-hat(L)(r))), las=1)
  }
)

这些建议可以满足您的需求吗?

答案 1 :(得分:0)

那里有很多代码,所以我可能会偏离基础,但看起来你可能可以使用sapply(xnew, "[[", "NofSimulations")从列表中获取NofSimulations元素。一旦它在矢量中,你可以尝试绘图吗?