简体   繁体   中英

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:

在此处输入图片说明

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM