可变长度核心名称识别

时间:2012-10-19 14:38:42

标签: r

我有一个包含以下行命名方案的数据集:

a.X.V
where:
a is a fixed-length core ID
X is a variable-length string that subsets a, which means I should keep X
V is a variable-length ID which specifies the individual elements of a.X to be averaged
. is one of {-,_}

我要做的是获取所有a.X's的列平均值。样本:

sampleList <- list("a.12.1"=c(1,2,3,4,5), "b.1.23"=c(3,4,1,4,5), "a.12.21"=c(5,7,2,8,9), "b.1.555"=c(6,8,9,0,6))
sampleList
$a.12.1
[1] 1 2 3 4 5

$b.1.23
[1] 3 4 1 4 5

$a.12.21
[1] 5 7 2 8 9

$b.1.555
[1] 6 8 9 0 6

目前我手动gsubbing .Vs以获取一般列表:

sampleList <- t(as.data.frame(sampleList))
y <- rowNames(sampleList)
y <- gsub("(\\w\\.\\d+)\\.d+", "\\1", y)

有更快的方法吗?

这是我在工作流程中遇到的2个问题的一半。另一半回答here

3 个答案:

答案 0 :(得分:2)

您可以使用模式向量来查找要分组的列的位置。我包含了一个我知道不匹配的模式,以表明解决方案对于这种情况是稳健的。

# A *named* vector of patterns you want to group by
patterns <- c(a.12="^a.12",b.12="^b.12",c.12="^c.12")
# Find the locations of those patterns in your list
inds <- lapply(patterns, grep, x=names(sampleList))
# Calculate the mean of each list element that matches the pattern
out <- lapply(inds, function(i) 
  if(l <- length(i)) Reduce("+",sampleList[i])/l else NULL)
# Set the names of the output
names(out) <- names(patterns)

答案 1 :(得分:2)

也许你可以考虑搞乱你的数据结构,以便更容易地应用一些标准工具:

sampleList <- list("a.12.1"=c(1,2,3,4,5), 
  "b.1.23"=c(3,4,1,4,5), "a.12.21"=c(5,7,2,8,9), 
   "b.1.555"=c(6,8,9,0,6))
library(reshape2)
m1 <- melt(do.call(cbind,sampleList))
m2 <- cbind(m1,colsplit(m1$Var2,"\\.",c("coreID","val1","val2")))

结果如下:

head(m2)
  Var1    Var2 value coreID val1 val2
1     1  a.12.1     1      a   12    1
2     2  a.12.1     2      a   12    1
3     3  a.12.1     3      a   12    1

然后你可以更轻松地做这样的事情:

aggregate(value~val1,mean,data=subset(m2,coreID=="a"))

答案 2 :(得分:1)

如果你只是转移到R而不是data.frame

list准备做这些事情。将'a','X'和'V'放入自己的列中。然后,您可以使用avebyaggregatesubset等。

data.frame(do.call(rbind, sampleList), 
           do.call(rbind, strsplit(names(sampleList), '\\.')))

#         X1 X2 X3 X4 X5 X1.1 X2.1 X3.1
# a.12.1   1  2  3  4  5    a   12    1
# b.1.23   3  4  1  4  5    b    1   23
# a.12.21  5  7  2  8  9    a   12   21
# b.1.555  6  8  9  0  6    b    1  555
相关问题