2014-11-13 4 views
0

이것은 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의 열 번째 열에서 값이 "포함한다"가, I는 검사하고자하는 경우, 해당 값에 행렬 y의 첫 번째 열은 행렬 l의 "start"와 "stop"값 사이에 있습니다. 행렬 y의 값이 행렬 l의 값 위에 있거나 값 사이에 있으면 행렬 y에서 값 "include"를 "exclude"로 바꿉니다. 중첩 된 for 루프로 할 수 있지만 더 우아하고 빠른 방법을 알고 싶었습니다. 행렬의 길이가 동일하지 않습니다. 고맙습니다.

+0

[여기] (http://stackoverflow.com/questions/24480031/roll-join-with-start-end-window)와 같은 병합을 고려할 수 있습니다. – MrFlick

답변

0

효과가 있지만 느립니다.

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") 
      } 
     } 
    } 
} 
관련 문제