重新采样两个列表之间的重叠

时间:2014-10-08 14:18:32

标签: r overlap random-sample resampling

我有两个.txt文件。这两个文件都是字符串列表,每行一个字符串,例如。

GRIM1
PHOXA2
SLITRK4

两个文本文件都长约20,000行。我想从文件1中随机抽取500个字符串,从文件2中随机抽取700个字符串。 然后我想计算与这两个子集重叠的字符串数量 然后我想重复这个过程100次并计算从100次重采样中重叠这些子集的最小,最大和平均字符串数。

我试图调整一些曾经用于类似任务的代码,但是我收到了一个错误:

Error in sample.int(length(x), size, replace, prob) : 
  cannot take a sample larger than the population when 'replace = FALSE'

此代码为:

listA <- read.csv(file="file1.txt", header=F)
listB <- read.csv(file="file2.txt", header=F)

listA <- as.character(listA) # to check that you really have a vector of gene names #maybe you have to do: listA <- as.character(listA)
listB <- as.character(listB) 

res <- rep(NA, 100) 
genesToDraw <- 500 # how many to select 
genesToDraw2 <- 700 # if you want to take different number from second list

for(i in 1:length(res)){

drawA <- sample(x=listA, size=genesToDraw, replace=FALSE)
drawB <- sample(x=listB, size=genesToDraw2, replace=FALSE) # or size=genesToDraw2

res[i] <- length(intersect(drawA, drawB))
}

hist(res, breaks=20)
table(res)
max(res)
sum(res > 5) # how often i

先谢谢你的帮助,如果我要澄清,请告诉我。

当我在代码的as.character部分之后运行dput(listA)和dput(listB)时,我收到一堆逗号分隔的数字作为输出。这是一个子集:

1100, 4576, 7394, 1343, 4997, 13807, 1233, 9580, 15254, 10466, 3333, 622, 11177, 4067, 4800, 7592, 5363, 9646, 11213, 14314, 2475, 8389, \n12559, 12808, 5248, 10423, 7856, 12976, 9695, 1674, 2090, 9369, 12089, 13952, 1218, 7966, 6949, 4088, 623, 4768, 2002, 11776, 14710, 5502, 6212, 7300, 2123, 7194, 2128, 1683, 14987, 4491, 2672, 10275, 9424, 997, 15506, 14307, 2644, 11508, 9272, 5107, 10146, 11693, 1802, 652, 13073, 4268, 5435, 718, 4845

致以最诚挚的问候,

Rubal

1 个答案:

答案 0 :(得分:1)

正如我们所讨论的,首先是因为你期待字符串,所以在read.csv调用中将stringsAsFactors标志设置为false,这样你就不会搞乱因素

listA <- read.csv(file="file1.txt", header=FALSE, stringsAsFactors=FALSE)
listB <- read.csv(file="file2.txt", header=FALSE, stringsAsFactors=FALSE)

现在,您将拥有两个数据帧,每个数据帧都有一列字符对象。样本函数需要向量,因此我们可以通过

将我们的一列数据帧转换为向量
listA<-listA[,1]
listB<-listB[,1]

这应该让你的代码运行!

相关问题