tidy_kinship: Create a tidy version of a kinship matrix

Description Usage Arguments Value Examples

View source: R/tidy_kinship.R

Description

A square symmetric kinship matrix is transformed into a tibble, with a row per unique element in the kinship matrix, and three columns: ID of row, ID of column, and the kinship value.

Usage

1
tidy_kinship(kinship, sort = TRUE)

Arguments

kinship

The n-by-n symmetric kinship matrix

sort

If TRUE (default), rows are sorted ascending by kinship value. Otherwise, order is moving along the upper triangle row-by-row

Value

A tibble with n * ( n + 1 ) / 2 rows (the upper triangle, including the diagonal), and 3 columns with names: id1, id2, kinship.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# create a symmetric matrix
kinship <- matrix(
    c(
        0.5, 0.1, 0.0,
        0.1, 0.5, 0.2,
        0.0, 0.2, 0.6
    ),
    nrow = 3
)
# add names (best for tidy version)
colnames(kinship) <- paste0('pop', 1:3)
rownames(kinship) <- paste0('pop', 1:3)
# this returns tidy version
kinship_tidy <- tidy_kinship( kinship )
# test colnames
stopifnot( colnames( kinship_tidy ) == c('id1', 'id2', 'kinship') )
# test row number
stopifnot( nrow( kinship_tidy ) == 6 )
# inspect it
kinship_tidy

genio documentation built on July 27, 2021, 1:06 a.m.