如何循环列表列表并使用R(最好是高效)按名称访问行?

时间:2022-04-02 04:06:54

This is probably a newbie question:

这可能是一个新手问题:

I have an array (let us call it a) each each element of which look like this:

我有一个数组(让我们称之为a),每个元素都是这样的:

structure(list(rs6838241 = structure(list(Results = structure(c(-0.02015,
0.022242, -0.0111580707, 0.0028333, 0.01067,-2.01730, NA, 0.1704, 0.133291277489982,
0.6855, 0.32983, 0.4598227, 6.764345237e-05, 0.31353), .Dim = c(7L, 2L), 
.Dimnames = list(c("FVCpp_utah", "FEV1pp_utah",
"WallAreaPct_seg","Vida_15perc_Exp","pctEmph_Slicer", 
"FEV1_FVC_utah", "JointModel"), c("beta","pvalue"))), 
nobs = 1112L), .Names = c("Results", "nobs"))), .Names = "rs6838241")

I would like to find the element with smallest p-value.

我想找到p值最小的元素。

I did something like this:

我做了这样的事情:

m <- 1000
for (i in 1:1000){
  m <- min(m, (a[i]$'Results')['JointModel','pvalue'])
}

But it does not do anything!! I am confused why I cannot do this:

但它没有做任何事情!我很困惑为什么我不能这样做:

> (a[1])$'Results'
  NULL

while this works?

这有效吗?

> a$'rs13125929'$'Results'
[SNIPPED]

why this one does not work?

为什么这个不起作用?

> s<-'rs13125929'
> (a[s])$'Results'
NULL

Is there anyway to find the lowest p-value without looping?

反正有没有循环找到最低的p值?

Thanks,

谢谢,

2 个解决方案

#1


1  

You can use this:

你可以用这个:

min(sapply(a, function(x) x$Results['JointModel','pvalue']))

To find the index of the list element with minimum pvalue, use which.min instead of min.

要查找具有最小p值的list元素的索引,请使用which.min而不是min。

#2


0  

  1. Use Reduce instead of the loop for min (see also here)
  2. 使用Reduce而不是loop for min(另见此处)
  3. use a[[1]]$'Results' (note double braces)
  4. 使用[[1]] $'结果'(注意双括号)

#1


1  

You can use this:

你可以用这个:

min(sapply(a, function(x) x$Results['JointModel','pvalue']))

To find the index of the list element with minimum pvalue, use which.min instead of min.

要查找具有最小p值的list元素的索引,请使用which.min而不是min。

#2


0  

  1. Use Reduce instead of the loop for min (see also here)
  2. 使用Reduce而不是loop for min(另见此处)
  3. use a[[1]]$'Results' (note double braces)
  4. 使用[[1]] $'结果'(注意双括号)