Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Set Intersection - Fatal编程技术网

R 在每两个列表元素中绘制交点

R 在每两个列表元素中绘制交点,r,list,set-intersection,R,List,Set Intersection,给定一个包含16个元素的列表,其中每个元素都是一个命名的数字向量,我想绘制每2个元素之间名称相交的长度。就是,;元素1与元素2的交点,元素3与元素4的交点,等等 虽然我可以用一种非常乏味、低吞吐量的方式来做这件事,但我必须重复这种分析,所以我想用一种更编程的方式来做 例如,前2个列表元素的前5个条目为: topGenes[[1]][1:5] 3398 284353 219293 7450 54658 2.856363 2.654106 2.653845 2.635599

给定一个包含16个元素的列表,其中每个元素都是一个命名的数字向量,我想绘制每2个元素之间名称相交的长度。就是,;元素1与元素2的交点,元素3与元素4的交点,等等

虽然我可以用一种非常乏味、低吞吐量的方式来做这件事,但我必须重复这种分析,所以我想用一种更编程的方式来做

例如,前2个列表元素的前5个条目为:

topGenes[[1]][1:5]

3398   284353   219293     7450    54658 
2.856363 2.654106 2.653845 2.635599 2.626518 

topGenes[[2]][1:5]
1300    64581     2566     5026   146433 
2.932803 2.807381 2.790484 2.739735 2.705030 
这里,第一行数字是基因id&我想知道每对载体(一个治疗复制)有多少个共同点,比如说,前100个

我已尝试以以下方式使用lappy():

vectorOfIntersectLengths <- lapply(topGenes, function(x) lapply(topGenes, function(y) length(intersect(names(x)[1:100],names(y)[1:100]))))

vectorofIntersectLength这就是您要找的吗

# make some fake data
set.seed(123)
some_list <- lapply(1:16, function(x) {
  y <- rexp(100)
  names(y) <- sample.int(1000,100)
  y
})

# identify all possible pairs
pairs <- t( combn(length(some_list), 2) )
# note: you could also use:  pairs <- expand.grid(1:length(some_list),1:length(some_list))
# but in addition to a-to-b, you'd get b-to-a, a-to-a, and b-to-b

# get the intersection of names of a pair of elements with given indices kept for bookkeeping
get_intersection <- function(a,b) {
  list(a = a, b = b, 
       intersection = intersect( names(some_list[[a]]), names(some_list[[b]]) ) 
  )
}

# get intersection for each pair
intersections <- mapply(get_intersection, a = pairs[,1], b = pairs[,2], SIMPLIFY=FALSE)

# print the intersections
for(indx in 1:length(intersections)){
  writeLines(paste('Intersection of', intersections[[indx]]$a, 'and',
                   intersections[[indx]]$b, 'contains:', 
                   paste( sort(intersections[[indx]]$intersection), collapse=', ') ) )
}
#制作一些虚假数据
种子集(123)

一些列表可能会看到expand.grid,但我不太确定你在问什么。请提供
dput(topGenes)
或具有代表性的示例子集,好的。以下是前2个列表元素的前10个条目:抱歉,在回复时过早地点击“回车”。我编辑这篇文章是为了(希望)清晰。啊,你想要名称的交叉点——让我编辑解决方案来反映这一点。几乎!我可以看到结果向量中的相交长度,以及自相交。确实如此。输出看起来有点滑稽,但我要查找的相交元素的名称肯定在其中。当我运行代码时,输出crossions如下所示:List of 256$:List of 3..$a:int 1..$b:int 1..$crossion:chr[1:100]“3398”“284353”219293“7450”…$:3个列表$a:int 2..$b:int 1..$交叉点:chr[1:3]“121260”“3934”“29890“最后一行显示topGenes前两个列表元素名称的交叉点,我猜长度为100的第一个交叉点是第一个元素本身。不过还是管用的!:-)@我用打印例程更新了控制台的答案。通过将
con=
参数更改为
writeLines
,可以轻松地将其打印到文件a。这是一个不完整的表,所以XML或JSON可能是表示它的好方法,但是如果您只是手动使用它,您可以将输出复制或通过管道传输到任何适合您的地方。
# make some fake data
set.seed(123)
some_list <- lapply(1:16, function(x) {
  y <- rexp(100)
  names(y) <- sample.int(1000,100)
  y
})

# identify all possible pairs
pairs <- t( combn(length(some_list), 2) )
# note: you could also use:  pairs <- expand.grid(1:length(some_list),1:length(some_list))
# but in addition to a-to-b, you'd get b-to-a, a-to-a, and b-to-b

# get the intersection of names of a pair of elements with given indices kept for bookkeeping
get_intersection <- function(a,b) {
  list(a = a, b = b, 
       intersection = intersect( names(some_list[[a]]), names(some_list[[b]]) ) 
  )
}

# get intersection for each pair
intersections <- mapply(get_intersection, a = pairs[,1], b = pairs[,2], SIMPLIFY=FALSE)

# print the intersections
for(indx in 1:length(intersections)){
  writeLines(paste('Intersection of', intersections[[indx]]$a, 'and',
                   intersections[[indx]]$b, 'contains:', 
                   paste( sort(intersections[[indx]]$intersection), collapse=', ') ) )
}