您可以使用[
包含数据框列的名称及其索引。所以foo[4]
将得到相同的结果foo["bias"]
(假如说bias
是第四列的名称)。
$bias
不是真的name该列的。$
只是 R 中的另一个函数,例如[
,用于访问数据帧的列(除其他外)。
但现在我要冒险就你的数据结构提供一些建议。如果嵌套列表的每个元素都包含唯一组合的数据station
and member
,这是数据的简化玩具版本:
dat <- expand.grid(station = rep(1:3,each = 2),member = rep(1:3,each = 2))
dat$bias <- sample(50:100,36,replace = TRUE)
tmp <- split(dat,dat$station)
tmp <- lapply(tmp,function(x){split(x,x$member)})
> tmp
$`1`
$`1`$`1`
station member bias
1 1 1 87
2 1 1 82
7 1 1 51
8 1 1 60
$`1`$`2`
station member bias
13 1 2 64
14 1 2 100
19 1 2 68
20 1 2 74
etc.
tmp
是一个长度为 3 的列表,其中每个元素本身就是一个长度为 3 的列表。每个元素都是一个数据框,如上所示。
将此类数据记录为单个数据帧确实要容易得多。你会注意到我首先是这样构建的(dat
),然后将其分割两次。在这种情况下,您可以rbind
使用如下代码再次将所有内容组合在一起:
newDat <- do.call(rbind,lapply(tmp,function(x){do.call(rbind,x)}))
rownames(newDat) <- NULL
在这种形式中,这些类型的计算是much easier:
library(plyr)
#Find the max bias for each unique station+member
ddply(newDat,.(station,member),summarise, mx = max(bias))
station member mx
1 1 1 87
2 1 2 100
3 1 3 91
4 2 1 94
5 2 2 88
6 2 3 89
7 3 1 74
8 3 2 88
9 3 3 99
#Or maybe the max bias for each station across all members
ddply(newDat,.(station),summarise, mx = max(bias))
station mx
1 1 100
2 2 94
3 3 99