Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 将表的多行与多个条件匹配_R_Loops - Fatal编程技术网

R 将表的多行与多个条件匹配

R 将表的多行与多个条件匹配,r,loops,R,Loops,我创建了一个如下循环,以检查与表中的行匹配的条件。如果它们匹配,则打印行名称。如果他们不匹配,什么都不会发生 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 (

我创建了一个如下循环,以检查与表中的行匹配的条件。如果它们匹配,则打印行名称。如果他们不匹配,什么都不会发生

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
条件1651838 1 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)
    }
}
条件如何:

## 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
##组成数据

z二元函数中的%in%应返回所有匹配的行:

> 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"
>行名(表)[strtab%处于%状态]
[1] "1651838" "1653006" "1656415"
>tabl2行名(tabl2)[strtab%处于%状态]
[1] "1651838" "1653006" "1656415" "1656415"

(听说match不会这样做,我有点惊讶,因为%in%的核心是match。)

看起来非常类似于对它的重新编写,这是一个真正的解决方案,但是如果z有两个相同的行,则只显示第一行。修改后的版本适合您吗?(注:我不知道你是否可以更改问题的标题,但它似乎不太具有描述性:我会说“将表的多行匹配到多个条件”——正确的答案不需要涉及for循环,事实上可能不会。。。