这是一个R问题。
我有两个矩阵"y“和"l":
> head(y)
SNP Category
1 29351 exclude
2 29357 exclude
3 29360 exclude
4 29372 include
5 29426 include
6 29432 include
> head(l)
start stop
1 246 11012
2 11494 13979
3 14309 18422
4 20728 20995
5 21457 29345
6 30035 31693如果矩阵y中的一行在第二列中有值"include“,我想检查矩阵y中第一列中的相应值是否位于矩阵l中的"start”和"stop“值上或之间。如果矩阵y中的值确实位于矩阵l中的值上或之间,则在矩阵y中将值"include”替换为"exclude“。我想我可以使用嵌套的for循环来做这件事,但我想知道一种更优雅更快的方法。矩阵的长度是不相等的。谢谢。
发布于 2014-11-13 13:02:39
这起作用了,但速度很慢。
y <- read.csv(file="SNP_pos_categorised0.99cutoff.csv", header=T)
l <- read.csv("SNPsToMoveFromINCLUDEtoEXCLUDE.csv", header=T)
colnames(y)
#[1] "SNP" "Category"
levels(y$Category)
#[1] " exclude" " include"
colnames(l)
#[1] "start" "stop"
#start processing
for(i in 1:nrow(y))
{
if(y[i,"Category"]==" include")
{
for(j in 1:nrow(l))
{
if(y[i,"SNP"] >= l[j,"start"] & y[i,"SNP"]<= l[j,"stop"])
{
y[i, "Category"] <- replace(y[i,"Category"], y[i,"Category"]==" include", " exclude" )
}
}
}
}https://stackoverflow.com/questions/26900865
复制相似问题