Home > Blockchain >  Applying a function to every combination of rows in a matrix using R | common connections
Applying a function to every combination of rows in a matrix using R | common connections

Time:10-18

I am new in R and I want to calculate the common and unique- connections of the components (CM) for my matrix.

Therefore, I have a matrix of Components(CM) and Workstations(WS). To calculate the common connection, I want to find out which component is produced by which workstation(as shown in the matrix) in comparison to the other components. Common connections = when CM1 and CM2 are produced by the same workstation unique connection = when CM1 is produced by the workstation and CM2 not.

Example
     WS1 WS2 WS3 WS4 WS5 WS6 WS7 WS8 WS9 WS10
CM1    1   0   1   1   1   1   1   1   0    0
CM2    0   1   0   0   0   1   1   1   0    0
CM3    1   0   0   1   1   0   1   0   0    1
CM4    0   1   1   1   1   0   1   1   1    1
CM5    1   0   0   1   0   0   0   1   0    1
CM6    0   1   0   1   0   0   0   0   1    0
CM7    0   1   1   1   1   1   1   0   1    1
CM8    1   0   0   1   1   1   1   1   1    0
CM9    1   0   0   1   1   1   0   1   1    0
CM10   1   1   0   0   1   0   0   1   0    0

I need a function, which compares each element of a row (for instance : CM1) with another row(for instance : CM2). For instance if CM1[6] & CM2[6] >= 1 the sum is to be formed. For example the common connection of CM1_2 would be 3. Additionally, I also need this for the uncommon connections, whereas CM1[1] >0 & CM[2] == 0.

For vectors, it should work like this:

a = CM1 #as a vector
b = Cm2 #as a vector
common <- sum(a&b >=1)  #common connection
only_inCM1 <- sum(a >0 & b ==0) #unique connections
only_inCM2 <- sum(b >0 & a ==0)

My current Problem is to create all the combinations of the common and uncommon connections. So the function needs to compare CM1 with CM2, CM1 with CM3...until CM10 with CM9, calculating the common connection and the unique connections and afterwards for each comparison (like. CM1_2) it should apply the function C[n] on top.

C[n] = 100 * common_connection/ common_connection unique connection

In the end, I want to calculate the sum of C[n] for all components divided by the number of Components. To have a general Index.


example for CM1 and CM2

     WS1 WS2 WS3 WS4 WS5 WS6 WS7 WS8 WS9 WS10
CM1    1   0   1   1   1   1   1   1   0    0
CM2    0   1   0   0   0   1   1   1   0    0

common connection CM1_2 = 3
only in CM1 = 4
only in CM2 = 1
C[1,2] = 100 * 3 / 3 4 = 42,85  # Index for CM1 
C[2,1] = 100* 3 / 3 1  = 75  # Index for CM2

C = (C[1,2]   C[2,1]) / 2 = (42,85   75)/2 = 58,87

I hope you have an advice or an hint help me, many thanks in advance :)

CodePudding user response:

Same here writing r first time, hope this helps you:

colnames = c("WS1","WS2","WS3","WS4","WS5","WS6","WS7","WS8","WS9","WS10")
rownames  = c("C1", "C2")
   
A = matrix(
 c(1,0,1,1,1,1,1,1,0,0,0,1,0,0,0,1,1,1,0,0),
 nrow = 2,  
 ncol = 10,        
 byrow = TRUE,
 dimnames = list(rownames, colnames)        
)
#no of rows
r = nrow(A)
print(A)

#Common Connection
cc = matrix(0,r,r)

#Unique Connection
uc = matrix(0,r,r)

# Loop over matrix
for(row1 in 1:(r-1)) {
    for(row2 in (row1 1):r) {
        for(col in 1:ncol(A)) {
            if (A[row1, col]   A[row2,col] == 2) {
                cc[row1,row2] = 1   cc[row1,row2]
                cc[row2,row1] = 1   cc[row2,row1]
            } else if (A[row1, col] == 0 & A[row2,col] == 1) {
                uc[row2,row1] = 1   uc[row2,row1]
            } else if (A[row1, col] == 1 & A[row2,col] == 0) {
                uc[row1,row2] = 1   uc[row1,row2]
            }
        }
    }
}

#c[n] calculation
cn1 = matrix(0,r,r)
cn = 0

for(row in 1:r) {
    for(col in 1:r) {
        if (cc[row,col] > 0) 
            cn1[row,col] = 100 * cc[row,col] / (uc[row,col]   cc[row,col])
        cn = cn   cn1[row,col]
    }
}

cn = cn / r

print(cc)
print(uc)
print(cn1)

#Final results asked
print(cn)

Might be not optimal but will work for you I hope.

CodePudding user response:

I have found a way, that works for my Problem quite well as i think.

#cn = common connection
#un_CM1 = unique connection for Component 1
#un_CM2 = unique connection for Component 2

data <- matrix

alpha = do.call(rbind, combn(nrow(data), 2, function(x) 
  data.frame(CM_1 = x[1], CM_2 = x[2], 
             cn = sum(data[x[1],]&data[x[2],] >= 1), 
             un_CM1 = sum(data[x[1],] >0 & data[x[2],] ==0),
             un_CM2= sum(data[x[2],] >0 & data[x[1],] ==0)), 
  simplify = FALSE))

alpha <- alpha %>%rowwise() %>%
  mutate(PCN_CM1 = (100*cn)/(cn un_CM1),PCN_CM2 =(100*cn)/(cn un_CM2) )

PCN <- sum(alpha$PCN_CM1 alpha$PCN_CM2) / (2* nrow(alpha))

  • Related