Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 创建矩阵列表,其中列是基于列表的排列_R_Lapply - Fatal编程技术网

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

R 创建矩阵列表,其中列是基于列表的排列,r,lapply,R,Lapply,我有两个列表,list1和list2。前者由簇值组成,后者由簇值组成。每个列表中的元素都是严格相关的,例如list1[[1]]有13个数字,list2[[1]]有13个p值,对应于list1中的每个值。然后,我有另一个列表,list3,根据list1中的数字分类了一定数量的pv值(在我的实际案例中至少有100个),也就是说,list3['1']将包含与1值对应的list2中的所有pv值。以下是一些对象的示例,以澄清问题 list1 # $cluster.1 # [1] 1 2 12 58 31

我有两个列表,
list1
list2
。前者由簇值组成,后者由簇值组成。每个列表中的元素都是严格相关的,例如
list1[[1]]
有13个数字,
list2[[1]]
有13个p值,对应于
list1
中的每个值。然后,我有另一个列表,
list3
,根据
list1
中的数字分类了一定数量的pv值(在我的实际案例中至少有100个),也就是说,
list3['1']
将包含与
1
值对应的
list2
中的所有pv值。以下是一些对象的示例,以澄清问题

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
包含与同一数字对应的所有PV值,例如数字2的所有PV值。到目前为止,我一直对建立一个矩阵列表感兴趣,在这个列表中,
list1
的元素被从
list3
中抽取的相同数量的元素所替代。我的命令是:

list4 <- lapply(list1, function(x) sapply(x, function(i, l) 
  sample(l[[as.character(i)]], 10, replace=T), l=list3))
但是,现在我想为每个集群构建排列,但避免在
list3
中对该集群的pv值进行采样。因此,作为一个例子,对于
list1['cluster.1]
,我想从
list3
中删除
list2['cluster.1']
中的pValue,对于
list1
中的其余集群也是如此

任何帮助都将不胜感激

非常感谢

更新

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

 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),]
最后,使用
list3
list1
我获得了一个列表(
list4
),其排列数为该集群中元素的行数和列数。到目前为止,我对使用相同的
mac
对整个基因组进行取样感兴趣,但现在我想从每个取样中删除该取样的聚类对应的pValue。 我用于排列的函数是:

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

list4你想要的可能没那么难——难的是费力地阅读你冗长而令人困惑的解释。这样行吗

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)
}))

你能不能
dput()
你的列表,也许还可以扩展一下你对期望结果的描述?你能在@vpipkt上发布生成这些列表的过程吗?我的数据@vpipkt非常庞大,列表1是一个2418个列表,包含许多元素变量(最小值=3,最大值=147),在我的例子中,list4必须是一个数据帧列表,有10000行和如此多的列作为其对应的list1和2中的元素,也就是说,如果list1[[2]]有4个元素,list3[[1]]将有10000行和4列的dim,我将发布@miles2know,处理后生成这些ListPost更新@miles2know这不是我想要的,但我还是会奖励你的
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`
list4 <- lapply(list1, function(x) sapply(x, function(i, l) {
  sample(l[[as.character(i)]], 10, replace=T)
}, l=list3))
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)
}))
list4 <- list()
for (mac in unique(pvals$mac)) {
    list4[[mac]] <- sample(pvals$pval[pvals$mac==m], 100, replace=FALSE)    
}