rtree

Calculate the total number of branches in a tree


I have the following matrix:

> M
     [,1] [,2] [,3] [,4]
[1,]    1    1    3    2
[2,]    2    2    1    1
[3,]    3    2    3    1
[4,]    2    2    2    2
[5,]    1    1    1    1
[6,]    3    2    3    2
[7,]    1    1    3    1
[8,]    2    1    1    1

Each row, in the file txt, has four positions separated by a space and represents the path of a tree. The tree consists of a root node and levels of additional nodes that form the hierarchy: the first and the third levels can have three nodes (1, 2 or 3); the remaining positions can assume only two values: 1 or 2.

Then, the tree described by previous example is the following:

enter image description here

I would calculate the total number of branches in the tree. For example, the tree depicted above has 21 branches in total.

My solution is the following:

nrow(unique( M[ , 1:2 ] ))+nrow(unique( M[ , 1:3 ] ))+nrow(unique( M[ , 1:4 ] ))

but it returns 18...


Solution

  • Here's a way to do it all in one line:

    sum(sapply(1:ncol(M), function(x) nrow(unique(M[, 1:x, drop = FALSE]))))
    

    As pointed out in comments, it seems like your issue is that you are not including the unique 1st column elements.