简体   繁体   English

在列表R中的数据框的同一列中找到最小值/最大值

[英]Find minimum/maximum within same column of dataframes in a list R

I have 323 data.frame s in a list called mrns . 我有323 data.frame S IN一个list称为mrns In each data.frame , there is a variable called raw.Systolic . 在每个data.frame ,都有一个名为raw.Systolic的变量。 What I need to do is find the minimum/maximum raw.Systolic among ALL 323 data.frame s. 我需要做的是在所有323个data.frame找到最小/最大raw.Systolic I can output the minima for each data.frame , but that's not what I need. 我可以为每个data.frame输出最小值,但这不是我所需要的。

The closest I've gotten so far is outputting the list of minima for each file and trying to find a way to find the minima of that output, but I don't know how. 到目前为止,我得到的最接近的结果是输出每个文件的最小值列表,并试图找到一种方法来找到该输出的最小值,但是我不知道如何。

This is what I have so far: 这是我到目前为止的内容:

for (i in 1:323) {
  print(min(mrns[[i]]$raw.Systolic, na.rm=TRUE))
}

Which outputs: 哪个输出:

[1] 86
[1] 109
[1] 114
[1] 104
[1] 115
...etc until 323 maximums are listed

When I do: 当我做:

for (i in 1:323) {
  mins <- min(mrns[[i]]$raw.Systolic, na.rm=TRUE)
}

I get: 我得到:

> mins
[1] 129

Does anyone have any suggestions? 有没有人有什么建议?

尝试,

min(unlist(lapply(mrns, function(x) min(x$raw.Systolic, na.rm=T))))

We can do this with: 我们可以这样做:

#minima:
do.call("min",sapply(mrns,getElement,name="raw.Systolic"))

#ranges
do.call("range",sapply(mrns,getElement,name="raw.Systolic"))

#NA-proof
do.call("min",c(sapply(mrns,getElement,name="raw.Systolic"),na.rm=T))

Note that getElement is just the extraction operator $ in function form, ie x$y is the same as getElement(object=x,name="y") . 注意, getElement只是函数形式的提取运算符$ ,即x$ygetElement(object=x,name="y")

If you know that all the elements of mrns have the same number of rows, just use min(sapply(...)) instead. 如果您知道mrns所有元素都具有相同的行数,请改用min(sapply(...))

Since sapply is likely to return a list given each element of mrns likely has a different number of rows, we use do.call with allows us to pass a list to min and range . 由于给定的mrns每个元素可能具有不同的行数,因此sapply可能返回一个list ,因此我们使用do.call with允许将list传递给minrange

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM