2017-02-18 109 views
1

我有一个数据框,并且我有一个ID列表,我想要搜索以检查它们是否在该数据框中。这个数据帧看起来这样:在R中的数据框的列中搜索多个元素

dput(bed,"mybed.bed") 
sample <- c("13874.p1", "13609.p1","12736.p1", "11970.p1","12025.p1","12189.p1","12529.p1","11522.p1","11716.p1","13684.p1") 

我想返回包含由sample矢量和df$sample_ID共享的值中的任何一个数据帧的行。

我试过使用sapply(samples, grepl, df$sample_ID),但它只是在检查样本的第一个元素存在时。任何帮助,将不胜感激!!

+0

sry误读了这个问题。你能提供样本数据吗? – BigDataScientist

+0

它在问题中。 – Workhorse

+0

很难进入R,你可以提供'dput()' – BigDataScientist

回答

0

我想我得到了一个解决方案,使用:str_locate_allstringr包。例如:

v <- c("abc11", "abc11abc11", "abc11abc11abc11abc") 
library(stringr) 
result1 <- str_locate_all(v[1], "11") 
result2 <- str_locate_all(v[2], "11") 
result3 <- str_locate_all(v[3], "11") 

输出将显示每场比赛一排的一对值的启动端(的匹配):

> result1 
[[1]] 
    start end 
[1,]  4 5 

> result2 
[[1]] 
    start end 
[1,]  4 5 
[2,]  9 10 

> result3 
[[1]] 
    start end 
[1,]  4 5 
[2,]  9 10 
[3,] 14 15 
> 

结果存储在一个小的不舒服结构:

> class(result3) 
[1] "list" 
> length(result3) 
[1] 1 
> 

这唯一元件是一个整数矩阵:

> class(result3[[1]]) 
[1] "matrix" 
> dim(result3[[1]]) 
[1] 3 2 
> 

功能str_locate提供更简单的输出,但它只会找到第一个匹配项。

我的建议将提取列表的第一要素,然后用它操作,例如:

m <- result3[[1]]

不是会更容易获得访问存储的信息对于result3矩阵3x2的:

> m 
    start end 
[1,]  4 5 
[2,]  9 10 
[3,] 14 15 

现在,要知道比赛的数量:

> nrow(m) 
[1] 3 

dim(m)[1]

因此,以矩阵形式存储的结果是更容易提取信息。为了让所有的输入参数匹配的位置刚刚提取的第一列:

> m[,1] 
[1] 4 9 14 

--------------------------------------------------------------------------------

编辑

应用以前的概念,原来的问题,即找到一个匹配项一个n值数组中的m-模式阵列。

--------------------------------------------------------------------------------

回到我的理解是你的问题,让我们说我们有以下的数据帧:

df = data.frame(ID = c(1,2,3,4), 
sample_ID = c(
    "12613.p1", 
    "12613.p1", 
    "11401.p1,11120.p1,11199.p1,11226.p1,11395.p1,11296.p1,11333.p1,11374.p1,11388.p1,11395.p1,11420.p1", 
    "11401.p1,13863.p1"), 
stringsAsFactors = F) 

现在,我们有以下的样本矢量:

sample <- c("11120.p1", "11395.p1", "12613.p1", "13863.p1", "11401.p1") 

df有4行,并且sample数组有5行。现在,根据前面的解释,让搜索在df$sample_ID,我们可以使用lapply功能找到的sample元素:

library(stringr) 
all <- sapply(df$sample_ID, FUN = function(x) {return(str_locate_all(x, sample))}) 

现在输出将是:

> class(all) 
[1] "matrix" 

其中

> dim(all) 
[1] 5 4 

因此,对于sample的每个元素,我们有5列,结果来自的给定行(四列)。

我们预计的sample以下匹配的每个元素:

Sample | df$sample_ID[1] | df$sample_ID[2] | df$sample_ID[3] | df$sample_ID[4] 
------- | -----------------|------------------|-----------------|--------------- 
11120.p1 |  0   |  0   |  1   |  0  
11395.p1 |  0   |  0   |  2   |  0  
12613.p1 |  1   |  1   |  0   |  0  
13863.p1 |  0   |  0   |  0   |  1  
11401.p1 |  1   |  0   |  1   |  0 

这是所获得的结果:

> all 
    12613.p1 12613.p1 
[1,] Integer,0 Integer,0 
[2,] Integer,0 Integer,0 
[3,] Integer,2 Integer,2 
[4,] Integer,0 Integer,0 
[5,] Integer,0 Integer,0 
    11401.p1,11120.p1,11199.p1,11226.p1,11395.p1,11296.p1,11333.p1,11374.p1,11388.p1,11395.p1,11420.p1 
[1,] Integer,2                       
[2,] Integer,4                       
[3,] Integer,0                       
[4,] Integer,0                       
[5,] Integer,2                       
    11401.p1,13863.p1 
[1,] Integer,0   
[2,] Integer,0   
[3,] Integer,0   
[4,] Integer,2   
[5,] Integer,2   
> 

矩阵的每个元素是一个list。以下是如何理解结果,对于每个[row, col]它提供了有关list元素的汇总信息:Integer,n将指示给定单元的元素数量。对于每场比赛,我们有两个值:[start,end],因此对于m比赛我们将有m x 2。这就是为什么[row, col] = [2,3]它的值为4

要提取的信息,可以说对于比赛的价值:(df$sample_ID[3]sample[2]=11395.p1第三排有:

> all[2,3] 
$`11401.p1,11120.p1,11199.p1,11226.p1,11395.p1,11296.p1,11333.p1,11374.p1,11388.p1,11395.p1,11420.p1` 
    start end 
[1,] 37 44 
[2,] 82 89 

提取所有匹配的位置:

> all[2,3][[1]][,1] 
[1] 37 82 

例如:m <- all[2,3][[1]] then:

> m[,1] 
[1] 37 82 

如何识别不匹配的情况?

让我们挑原有矩阵,其中没有匹配的元素[1,1],则:

> m <- all[1,1][[1]] 
> dim(m) 
[1] 0 2 
> dim(m)[1] 
[1] 0 
> 

我希望现在这解决您的具体问题。

1

呼叫:

unique(do.call(c, sapply(X = sample, FUN = function(x){return(grep(pattern = x,x = df$sample_id))}))) 

应该工作:

> df = data.frame(chrom = c(1,2,1,1), 
+     sample_id = c("12613.p1", "12613.p1","11118.p1,11120.p1,11199.p1,11226.p1,11285.p1,11296.p1,11333.p1,11374.p1,11388.p1,11395.p1,11420.p1", "11401.p1,13863.p1"), 
+     stringsAsFactors = F) 
> 
> 
> 
> sample <- c("13874.p1", "13609.p1","12736.p1", "11970.p1","12025.p1", 
+    "12189.p1","12529.p1","11522.p1","11716.p1","13684.p1") 
> 
> 
> unique(do.call(c, sapply(X = sample, FUN = function(x) {return(grep(pattern = x,x = df$sample_id))}))) 
integer(0) 

无解

但是,如果我最后一个字符串添加到样本:

> sample <- c("13874.p1", "13609.p1","12736.p1", "11970.p1","12025.p1", 
+    "12189.p1","12529.p1","11522.p1","11716.p1","13684.p1", 
+    "11199.p1") 
> 
> 
> unique(do.call(c, sapply(X = sample, FUN = function(x){return(grep(pattern = x,x = df$sample_id))}))) 
[1] 3 

它的作品!

0

我想我找到了一个简单的解决方案来解决这个问题(道歉为不张贴更真实的数据,我的数据集是巨大的)。

所以我有一个ID的字符向量,sample。然后我有一个表,其中一列包含每行ID的列表。

hits <- c() 
for(i in sample){ 
     hits <- append(hits, which(grepl(i, df$sample_ID, fixed = TRUE))) 
} 

hits2 <- unique(hits) 

我只是去通过sample向量,每一次我检查,看它是否在每个DF $ sample_ID列表中存在。它返回每个正点击的行数(来自数据框)。由于某些行可能有2个匹配项,因此我删除了重复项。

我可以基于这些行然后子集。

df2 <- df[hits2,]