Collapsing clades of 'foo' - huh?

I have posted this question at Stack Overflow. I hope this doesn't violate
any community rules about double posting.

I probably could have worded the title better, but I am wanting to collapse
any clade within a phylogenetic tree (even if the clade has one member)
which has a tip label of "foo” and then count the number of tips which were
dropped from that specific clade and create a branch with a tip label
displaying 35 foos.

the dropped tips do not maintain their position in the tree. Rather, they
are all grouped at the end (counted properly however). Is there anyway to
collapse a clade by tip labels and maintain its position"

When asked to clarify, the poster responded that what he or she wants
to do is convert a tree like this:

into a tree like this:

Florian Boucher posted a
solution
that probably works, I haven't tried. Here is another one:

This is our uncollapsed tree. Now let's go about identifying all the nodes
that we want to collapse and collapsing them. One complication is that each time
we collapse one clade, the node numbers will, of course, change. A further
wrinkle is that because tip labels repeat, it might be difficult to match
tips and nodes in the tipical way. Finally, we might want to maintain the
tip a certain height about the root - for instance the mean height of the
'foo's of its clade:

About this blog

This web-log chronicles the development of new tools for phylogenetic analyses in the phytools R package. Unless you a reading a very recent page of the blog, I recommend that you install the latest CRAN version of phytools (or latest beta release) before attempting to replicate any of the analyses of this site. That is because the linked functions may be archived, and very likely have been replaced by newer versions.