llply子集由应用于列表中元素的函数组成

时间:2014-09-21 01:52:03

标签: r subset plyr

我有一个列表,我在子集中失败了。这就是列表的样子:

dune.envfit <- 
structure(list(vectors = structure(list(arrows = structure(c(0.280345610462046, 
0.23065528788472, -0.529798086330133, 0.143997109594625, -0.164377286767545, 
-0.135605646341866, -0.963240459395111, -0.317899182650768, 0.128920508855905, 
0.219446302740393, 0.716407585418307, 0.787931102449639, 0.346775848714473, 
-0.237555622845223, -0.436638573595095, -0.218629125777214, -0.316702141990071, 
0.0503363265919834, 0.959899129437392, 0.973035527702261, -0.848123804477229, 
0.989578108300903, -0.986397540343111, -0.990762892260406, -0.26864068452916, 
-0.948124522238495, 0.991654931110784, 0.975624579545622, -0.697681998875642, 
0.615763410566507, -0.937948032008361, -0.971373937294294, -0.899637013494243, 
-0.975808026899703, -0.948525040923486, 0.998732323610899), .Dim = c(18L, 
2L), .Dimnames = list(c("TIC.mg.L", "Alkalinity.mg.L", "TOC.mg.L", 
"DIC.mg.L", "DOC.mg.L", "Na", "K", "Mg", "Ca", "Dissolved.N.mg.L", 
"Total.N.mg.L", "SI75", "FI", "HIX", "BIX", "EX.max", "MAXI", 
"MaxEX"), c("MDS1", "MDS2")), decostand = "normalize"), r = structure(c(0.59685535501548, 
0.185756858794004, 0.774378247785189, 0.19777058682652, 0.773575290577961, 
0.355910385488688, 0.808105377304065, 0.757415261345049, 0.808139825741362, 
0.756826061659786, 0.766773822773421, 0.297255075467242, 0.0482776303145529, 
0.458034365410462, 0.533467657077853, 0.757925150420371, 0.757494019430756, 
0.452226922967628), .Names = c("TIC.mg.L", "Alkalinity.mg.L", 
"TOC.mg.L", "DIC.mg.L", "DOC.mg.L", "Na", "K", "Mg", "Ca", "Dissolved.N.mg.L", 
"Total.N.mg.L", "SI75", "FI", "HIX", "BIX", "EX.max", "MAXI", 
"MaxEX")), permutations = 999, pvals = c(0.014, 0.418, 0.003, 
0.367, 0.003, 0.1, 0.002, 0.003, 0.001, 0.003, 0.003, 0.158, 
0.836, 0.042, 0.02, 0.003, 0.003, 0.095)), .Names = c("arrows", 
"r", "permutations", "pvals"), class = "vectorfit"), factors = NULL, 
    na.action = function (object, ...) 
    UseMethod("na.action")), .Names = c("vectors", "factors", 
"na.action"), class = "envfit")

我想提取列表中与任何pvals <0.05相关的每个元素。我知道pvals位于列表的第一个元素中。它们是名为Pr(&gt; r)的列,即使在列表的str()中它们也被称为pvals:

> dune.envfit[[1]]
                     MDS1     MDS2     r2 Pr(>r)    
TIC.mg.L          0.99242 -0.12290 0.5970  0.017 *  
Alkalinity.mg.L   0.91283  0.40833 0.1860  0.414    
TOC.mg.L         -0.55732  0.83030 0.7743  0.005 ** 
DIC.mg.L          0.42268  0.90628 0.1972  0.394    
DOC.mg.L         -0.52551 -0.85079 0.7742  0.003 ** 
Na               -0.38616 -0.92243 0.3543  0.096 .  
K                -0.36924  0.92934 0.8080  0.001 ***
Mg               -0.91182  0.41058 0.7574  0.004 ** 
Ca                0.35178  0.93608 0.8076  0.001 ***
Dissolved.N.mg.L  0.86231  0.50638 0.7572  0.003 ** 
Total.N.mg.L      0.26924 -0.96307 0.7671  0.005 ** 
SI75              0.41623 -0.90926 0.2971  0.165    
FI                0.18448 -0.98284 0.0486  0.824    
HIX              -0.95753 -0.28834 0.4569  0.056 .  
BIX              -0.65163  0.75853 0.5327  0.027 *  
EX.max           -0.86522 -0.50139 0.7572  0.006 ** 
MAXI             -0.91477  0.40398 0.7571  0.006 ** 
MaxEX             0.11461  0.99341 0.4542  0.070 .  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
P values based on 999 permutations.

到目前为止,我一直试图将llply应用于:

dune.envfit<-llply(dune.envfit, dune.envfit$vectors$pvals, summarize,  function(x) x<0.05)

但这不能解决错误&#34; llply错误(dune.envfit,dune.envfit $ vectors $ pvals,summarize,function(x)x&lt;:   .fun不是一个功能。&#34;我不知道如何为llply类型函数编写函数,我相信错误告诉了我。

谢谢。

在讨论了一些可能性之后,我现在只有我想要的那些。但是,我需要知道新列表的名称是什么,其中包含这些用于以后编码的pvals。

str(dune.envfit_subset)
List of 2
 $ : num [1:12, 1:2] 0.1125 -0.2401 -0.0644 -0.7822 -0.1268 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:12] "TIC.mg.L" "TOC.mg.L" "DOC.mg.L" "K" ...
  .. ..$ : chr [1:2] "MDS1" "MDS2"
 $ :List of 3
  ..$ r           : Named num [1:12] 0.597 0.774 0.774 0.808 0.759 ...
  .. ..- attr(*, "names")= chr [1:12] "TIC.mg.L" "TOC.mg.L" "DOC.mg.L" "K" ...
  ..$ permutations: num [1:12] 999 NA NA NA NA NA NA NA NA NA ...
  ..$ pvals       : num [1:12] 0.024 0.002 0.002 0.002 0.004 0.004 0.005 0.005 0.044 0.029 ...

1 个答案:

答案 0 :(得分:2)

听起来您想要对所有值进行子集,以使关联的p值小于0.05。

试一试。

> dv <- dune.envfit$vectors
> dvSubset <- c(arrows = list(dv$arrows[w <- which(dv$pvals < 0.05),]), 
                lapply(dv[-1], `[`, w))

看起来你也可以

> as.data.frame(dvSubset)[-4]

返回删除了permutations列的所需子集的数据框

> str(dvSubset)
List of 4
 $ arrows      : num [1:12, 1:2] 0.28 -0.53 -0.164 -0.963 -0.318 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:12] "TIC.mg.L" "TOC.mg.L" "DOC.mg.L" "K" ...
  .. ..$ : chr [1:2] "MDS1" "MDS2"
 $ r           : Named num [1:12] 0.597 0.774 0.774 0.808 0.757 ...
  ..- attr(*, "names")= chr [1:12] "TIC.mg.L" "TOC.mg.L" "DOC.mg.L" "K" ...
 $ permutations: num [1:12] 999 NA NA NA NA NA NA NA NA NA ...
 $ pvals       : num [1:12] 0.014 0.003 0.003 0.002 0.003 0.001 0.003 0.003 0.042 0.02 ...