Modify a dataframe with dependencies

1

I have a dataframe like this:

> print(A)
         5 3 7 6 11 4 20 8 16 18
     r01 1 1 1 1  1 1  1 1  1  0
     r02 0 0 0 0  0 0  0 1  1  0
     r03 1 0 1 0  1 1  0 0  0  0
     r04 0 1 0 0  0 1  0 1  0  1
     r05 1 1 0 1  1 1  0 0  1  1

And another like this:

    > print(B)
     head_req_ID tail_req_ID
1         r04         r01
2         r01         r02
3         r05         r03

For each column of A I have to check with the data of B.

B is a structure that indicates the requirements: it indicates that, in order to have 1 in r01 , it must be r04 a 1 . That is,

  

always has to be the element of the first column to be able to have the element of the second column,

and thus for all rows of B. I put a graphic example

EDIT

I tried the following:

checkImp <- function(x,y,z){

  print(c("Columna de A",x))
  print(c("Primer elemento de B",y))
  print(c("Segundo elemeno de B",z))

  print(c("Obtengo el primer índice",indice1 <- which(data$req_ID %in% y)))
  print(c("Obtengo el segundo índice",indice2 <- which(data$req_ID %in% z)))

   if(x[as.numeric(indice1)] == 0 & x[as.numeric(indice2)] != 0){
     x[indice2] <- 0
   }
}

mapply(checkImp,A,B[,1],B[,2])

Print output

[1] "Columna de A" "1"            "0"            "1"            "1"            "1"            "0"            "0"            "1"           
[10] "0"            "1"            "0"            "1"            "0"            "0"            "1"            "0"            "0"           
[19] "0"            "0"            "1" 

[1] "Primer elemento de B" "r04"                 
[1] "Segundo elemento de B" "r01" 

The problem is that I can only compare the column of A with the first row of B, I need to compare the column of A with ALL the data of B.

    
asked by Draggluon 02.07.2017 в 15:21
source

1 answer

0

For each row of A with the index in each row of B I check the values, I get the unmodified values in A first, valor1 checks for tail , if head is 1 tail it must be at 0 and in the other way for valor2 . Then I write in the structure.

checkExc <- function(A){

    for(i in 1:nrow(B)) {

      valor1 <- ifelse((A[B$head_req_ID[i],] == 1) , 0,A[B$tail_req_ID[i],])
      valor2 <- ifelse((A[B$tail_req_ID[i],] == 1) , 0,A[B$head_req_ID[i],])

      A[B$tail_req_ID[i],] <<- valor1
      A[B$head_req_ID[i],] <<- valor2

    }
  }


if(nrow(B)!=0){
  checkExc(A)
}
    
answered by 03.07.2017 в 19:28