YuLab-SMU / treeio

:seedling: Base Classes and Functions for Phylogenetic Tree Input and Output
https://yulab-smu.top/treedata-book/
94 stars 24 forks source link

add Inner_join method #93

Closed xiangpin closed 1 year ago

xiangpin commented 1 year ago

Description

add inner_join method for treedata or phylo class

Related Issue

There are no simliar issues. This is a new feature. The inner_join for treedata will keep the tip or observation from phylo that has a matching key in y.

Example

> tr <- ape::rtree(8)
> x <- data.frame(id = sample(tr$tip.label, 4), trait = rnorm(4))
> tr.da1 <- tr %>% inner_join(x, by=c('label'='id'))
> x2 <- data.frame(id=c('t1', 't1', 't2', 't3', 't2', 't4'), trait=c(10, 20, 30, 40, 50, 60))
> tr.da2 <- tr %>% inner_join(x2, by=c('label'='id'))
> tr.da1
'treedata' S4 object'.

...@ phylo:

Phylogenetic tree with 4 tips and 3 internal nodes.

Tip labels:
  t5, t6, t8, t2

Rooted; includes branch lengths.

with the following features available:
  '', 'trait'.

# The associated data tibble abstraction: 7 × 4
# The 'node', 'label' and 'isTip' are from the phylo tree.
   node label isTip  trait
  <int> <chr> <lgl>  <dbl>
1     1 t5    TRUE   0.478
2     2 t6    TRUE  -0.775
3     3 t8    TRUE   0.439
4     4 t2    TRUE   0.962
5     5 NA    FALSE NA
6     6 NA    FALSE NA
7     7 NA    FALSE NA
> tr.da2
'treedata' S4 object'.

...@ phylo:

Phylogenetic tree with 4 tips and 3 internal nodes.

Tip labels:
  t4, t3, t2, t1

Rooted; includes branch lengths.

with the following features available:
  '', 'trait'.

# The associated data tibble abstraction: 7 × 4
# The 'node', 'label' and 'isTip' are from the phylo tree.
   node label isTip trait
  <int> <chr> <lgl> <list>
1     1 t4    TRUE  <tibble [1 × 1]>
2     2 t3    TRUE  <tibble [1 × 1]>
3     3 t2    TRUE  <tibble [2 × 1]>
4     4 t1    TRUE  <tibble [2 × 1]>
5     5 NA    FALSE <NULL>
6     6 NA    FALSE <NULL>
7     7 NA    FALSE <NULL>
> tr.da2 %>% tidyr::unnest(trait)
# A tbl_df is returned for independent data analysis.
# A tibble: 6 × 4
   node label isTip trait
  <int> <chr> <lgl> <dbl>
1     1 t4    TRUE     60
2     2 t3    TRUE     40
3     3 t2    TRUE     30
4     3 t2    TRUE     50
5     4 t1    TRUE     10
6     4 t1    TRUE     20