2011-03-22 6 views
1

아래의 루프를 만들어 테이블의 행과 일치하는 조건을 확인하십시오. 일치하면 rowname이 인쇄됩니다. 일치하지 않으면 아무 일도 일어나지 않습니다.테이블의 여러 행을 여러 조건과 일치 시키려면

condition <- c(0,0,1,1) 
id <- apply(table, 1, 
      function(i) sum(i[1:length(table)] != condition)==0) 
idd<-as.matrix(id) 
for (i in 1:length(idd)){      
    if (idd[i] == TRUE) { 
     print(rownames(idd)[i]) 
    }    
} 

> table         
>   [1] [2] [3] [4]      
>1651838 1 1 0 0 
>1653006 0 0 0 0 
>1656415 0 0 0 1 
>1657317 -1 0 0 0 

내 질문은 : 여러 조건에 대해이 루프를 만들 수 있습니까? 예 :

condition <- c("0,0,0,0","0,0,0,1","0,0,1,0","0,1,0,0","1,0,0,0", 
       "0,0,1,1","1,1,0,0","0,1,1,0","1,0,0,1","1,0,1,0", 
       "0,1,0,1","1,0,1,1","1,1,0,1","1,1,1,0","0,1,1,1","1,1,1,1") 

for(r in 1:length(condition)){ 
    id <- apply(regulationtable, 1, 
       function(i) sum(i[1:length(regulationtable)] != condition[r])==0 
       ) 
    idd<-as.matrix(id) 
    test<-list() 
    for (i in 1:length(idd)) {      
     if (idd[i] == TRUE) { 
      print(rownames(idd)[i]) 
     }    
     test[[i]]<-matrix(idtest) 
    } 
} 

감사합니다.

+1

http://stackoverflow.com/questions/5375642/venestiagram-create-list-of-venncounts –

답변

1

방법에 대해 :

## make up data 
z <- matrix(c(1,0,0,-1,1, 
       1,0,0,0,1, 
       0,0,0,0,0, 
       0,0,1,0,0), 
      nrow=5, 
      dimnames=list(LETTERS[1:5],NULL)) 

condition <- c("0,0,0,0","0,0,0,1","0,0,1,0","0,1,0,0","1,0,0,0", 
       "0,0,1,1","1,1,0,0","0,1,1,0","1,0,0,1","1,0,1,0", 
       "0,1,0,1","1,0,1,1","1,1,0,1","1,1,1,0","0,1,1,1","1,1,1,1") 

strtab <- apply(z,1,paste,collapse=",") 
## rownames(z)[match(condition,strtab)] ## first match only 
omat <- outer(condition,strtab,"==") ## all comparisons 
colnames(omat)[col(omat)][omat]  ## select corresponding colnames 

?

+0

의 rewording과 매우 유사합니다. 사실 해결책이지만, z에 두 개의 동일한 행이있는 경우에만 첫 x 째 행이 표시됩니다. – Lisann

+0

수정 된 버전이 효과가 있습니까? (추신 : 당신이 질문의 제목을 바꿀 수 있는지 모르겠다. 그러나 그것은 매우 서술 적으로 보이지 않는다 : 나는 "여러 조건에 여러 행을 매치"라고 말했을 것이다 - 정답은 필요하다. 루프를 포함하지 않고 실제로는 그렇지 않습니다 ... –

2

일치하는 모든 행을 반환해야 % 이항 함수의 %

> rownames(table)[strtab %in% condition] 
[1] "1651838" "1653006" "1656415" 

> tabl2 <- table[c(1:4, 3), ] 
> rownames(tabl2)[strtab %in% condition] 
[1] "1651838" "1653006" "1656415" "1656415" 

(나는하지 않을 것이라고 일치를 듣고 놀랐 조금 해요, 그 %의 %가에서 경기로 기록되어 있기 때문에 그것의 핵심.)

+0

당신이 옳습니다. 이것이 내가 필요한 것입니다. 감사합니다! – Lisann

+0

내 것보다 낫다 ... –