创建矩阵列表,其中列是基于列表的排列

时间:2014-10-29 01:25:36

标签: r lapply

我有两个列表,list1list2。前者由簇值组成,后者由簇pvalues组成。每个列表中的元素是严格相关的,例如list1[[1]]有13个数字,list2[[1]]有13个pvalues对应list1中的每个值。然后,我有另一个列表,list3,根据list1中的数字分类了一定数量的p值(在我的实际情况中至少有100个),即list3['1']将有来自list2的所有pvalues,对应的值为1。以下是对象的一些示例,以澄清问题。

list1
# $cluster.1
# [1] 1 2 12 58 31 41 44 24
# 
# $cluster.2
# [1] 6  56  46  44
#
# $cluster.3
# [1]   1  63  74   4 12
#
# $cluster.4
# [1]  49 112   9  34   4  76  48  18  20  64
#
# $cluster.5
# [1]  14  22  63  47  36   6  40   7   2   4  90  16  20  15  14 18  76  35
#
# $cluster.6
# [1]    1    9    1    8    2    2   51   36    3  212   33   12   88 23

list2
# $cluster.1
# [1] 0.6591487 0.8994453 0.1538042 0.6964092 0.8401874 0.3814041 0.4633218
# [8] 0.7244993
#
# $cluster.2
# [1] 0.8497138 0.5865632 0.1077595 0.6833493
#
# $cluster.3
# [1] 0.3361554 0.6120117 0.0981049 0.5463973 0.3299392
#
# $cluster.4
# [1] 0.66537320 0.92404972 0.03616409 0.20704537 0.40120409 0.68727494
# [7] 0.60326315 0.08871090 0.71780273 0.09714994
#
# $cluster.5
# [1] 0.5926167 0.4155177 0.5230090 0.3620749 0.8698867 0.2490805 0.2775648
# [8] 0.1876079 0.5346257 0.6736455 0.3626760 0.8941776 0.4278336 0.7944475
# [15] 0.6687182 0.0171974 0.2931373 0.3987727
#
# $cluster.6
# [1] 0.3222530 0.1097813 0.3014139 0.9999900 0.5232969 0.4544731 0.4342567
# [8] 0.9999900 0.5435826 0.1937477 0.1713069 0.7474790 0.1683223 0.8814443

list3[1:2]
# $`1`
# [1] 0.2977049 0.3080035 0.3445133 0.2938342 0.3630210 0.3037416 0.2841442
# [8] 0.2777617 0.3366143 0.3121525 0.2460582 0.3229141 0.3283752 0.4038269
# [15] 0.3220467 0.3059212 0.2960296 0.3747395 0.3228451 0.2894994 0.3609505
# [22] 0.3447814 0.2993272 0.3088115 0.3255970
#
# $`2`
# [1] 0.21775479 0.98620413 0.25035841 0.31131319 0.48057769 0.98633571
# [7] 0.20208590 0.39117415 0.55579118 0.23737710 0.37548844 0.20139280
# [13] 0.49689904 0.34500830 0.19796570 0.45113871 0.20210998 0.51241253
# [19] 0.49254870 0.50922946 0.20125218 0.21230656 0.23612062 0.13508699
# [25] 0.48944306 

list3基于前两个列表,即list1的第一个元素是与list2的第一个元素的pvalue相关联的数字。因此,list3包括对应于相同数字的所有p值,例如数字2的所有p值。到目前为止,我有兴趣构建一个矩阵列表,其中list1的元素被替换通过采样来自list3的元素具有相同的数字。我的命令是:

list4 <- lapply(list1, function(x) sapply(x, function(i, l) 
  sample(l[[as.character(i)]], 10, replace=T), l=list3))

这个命令给了我这种输出

list4[[2]]
#           [,1]       [,2]      [,3]      [,4]
# [1,] 0.7983852 0.29404183 0.2416229 0.3018420
# [2,] 0.7398054 0.41266109 0.9253389 0.3249007
# [3,] 0.1150981 0.24138907 0.4040050 0.8335943
# [4,] 0.4564887 0.28336511 0.2520307 0.5460348
# [5,] 0.2810911 0.92870457 0.6865136 0.4851188
# [6,] 0.6396584 0.68957506 0.8336891 0.1355544
# [7,] 0.3365557 0.01609222 0.2504679 0.1974983
# [8,] 0.2307067 0.99999000 0.8328432 0.6538944
# [9,] 0.9999900 0.84980684 0.5590235 0.2566799
# [10,] 0.5791690 0.27672559 0.3584696 0.8335943

但是,现在我想为每个集群构建排列,但是避免在list3中对该集群的pvalue进行采样。因此,作为示例,对于list1['cluster.1],我想从list3中移除list2['cluster.1'] pvalues,并在list1中移除其他群集中的pvalues。

非常感谢任何帮助。

非常感谢

UPDATE,

以下评论我将更新流程以获取这些列表和最终的愿望输出。这些列表来自一个包含~17,000行和3列的data.frame,示例如下所示:

 head(pvals)
#    gene        pval mac
#1   A1CF 0.896076585  26
#2  ABCC2 0.376808322 571
#3   ABI1 0.048601644  27
#4 ABLIM1 0.729589080  63
#5 ACADSB 0.001609905  50
#6  ACBD5 0.446628090  11

list3来自pvals data.frame中的列mac分割:

split.mac = split(pvals, pvals[,3])
mac.pval = lapply(split.mac, '[[', 2)
pvals.order <- pvals[order(pvals$mac),]

然后,我生成一个包含每个元素至少100个基因的列表

l3 <- list()
ll1 <- length(mac.pval)
length(l3) <- ll1
set.seed(4)
for (i in 1:ll1) {
  vec1 <- mac.pval[[i]]
  jl <- 1; jr <- 1;
  while (length(vec1) < 100) {
    if(i==1 || i-jl==0) {
      vec1 <- c(vec1, mac.pval[[i+jr]])
      jr <- jr+1
    } else if (i==ll1 || jr+i==ll1 ) {
      vec1 <- c(vec1, mac.pval[[i-jl]])
      jl <- jl+1
    } else {
      vec1 <- c(vec1, mac.pval[[i-jl]], mac.pval[[i+jr]])
      jl <- jl+1
      jr <- jr+1
    } 
  } 
  l3[[i]] <- vec1  
}

names(l3) <- names(mac.pval)
list3 <- l3

clusters <- strsplit(readLines("clusters.txt"), "\t") # the output is a list with gene names

然后,list1和&#39; list2 are generated matching the pvalues` data.frame

list2 <- lapply(clusters, function(x) {
  pvals[match(as.character(unlist(x)), as.character(pvals[[1]])), 2]
}) # the output is a list with the `mac` column of `pvals`

list1 <- lapply(clusters, function(x) {
  pvals[match(as.character(unlist(x)), as.character(pvals[[1]])), 3]
}) # the output is a list with the `pval` column of `pvals`

最后,使用list3list1我获得了一个列表(list4),其中排列数是行和列,是该群集中元素的数量。到目前为止,我对使用相同mac的整个基因组进行采样感兴趣,但是现在我想从每个采样中删除对于该采样在群集中对应的p值。 我用于排列的功能是:

list4 <- lapply(list1, function(x) sapply(x, function(i, l) {
  sample(l[[as.character(i)]], 10, replace=T)
}, l=list3))

希望此更新有助于获得答案,

非常感谢

1 个答案:

答案 0 :(得分:0)

你想要的可能并不那么困难 - 困难的是趟过你冗长而令人困惑的解释。这会有用吗?

list4 <- lapply(list1, 
  function(x) sapply(x, function(i) {
    sample_from <- setdiff(list3[[as.character(i)]], list1[[as.character(i)]])
    sample(sample_from, 10, replace=T)
}))

一般来说,你可能会以错误的方式解决这个问题。例如,为什么还要生成列表1和2呢?您已经拥有数据框中的数据,因此请在那里工作。 我认为您的目标是list3的每个唯一值list1的样本。 list3是一堆信息。所以我猜你想要做一些更简单的事情:

list4 <- list()
for (mac in unique(pvals$mac)) {
    list4[[mac]] <- sample(pvals$pval[pvals$mac==m], 100, replace=FALSE)    
}
相关问题