R/07_traitgaps.R

######################################################################
#
# GRM -- identify gaps in trait measurements
#
#  Rob Smith, phytomosaic@gmail.com, 10 Apr 2020
#
##  CC-BY-SA 4.0 License (Creative Commons Attribution-ShareAlike 4.0)


require(data.table)
load('./data/pnw_traits.rda', verbose=T)
d <- pnw_traits  ;  rm(pnw_traits)
d$traitname <- ecole::clean_text(d$traitname)
table(d$traitid)

unique(data.frame(traitid=d$traitid, traitname=d$traitname))

d$observationid[d$dataid == 327] # 'experimental' (not natural)

### TODO: make wide if needed:
# 'observationid' identifies measurements on the same entity
`f` <- function (data, ...) {
        if (!is.data.frame(data))
                stop("must be dataframe")
        if (ncol(data) != 3)
                stop("must have 3-column format")
        x <- data[, 1]
        y <- data[, 2]
        z <- data[, 3]
        fx <- as.factor(x)
        fy <- as.factor(y)
        m <- matrix(NA, nrow = nlevels(fx), ncol = nlevels(fy),
                    dimnames = list(levels(fx), levels(fy)))
        m[cbind(fx, fy)] <- z
        # list(x = sort(unique(x)), y = sort(unique(y)), z = m)
        # data.table::as.data.table(m)
        m
}
w <- f(d[,c('observationid','traitname','origvaluestr')])
# rows = one observationid, cols = traits, elements = trait vals
str(w)
phytomosaic/pnw documentation built on April 16, 2020, 7:29 p.m.