R/get_pop_tpts.R

Defines functions get_pop_tpts

Documented in get_pop_tpts

##* GET POPULATION THERMAL PERFORMANCE TRAITS

#' Get population thermal performance traits
#'
#' Gets an entire populations thermal performance traits from TP data.
#'
#' @param TPD A population's temperature performance dataset with ID, Tm & P columns
#'
#' @param Degree Degree of the polynomial regression that will be used to extract the data.
#'
#' @param Pmin The minimal thermal performance. From it CTmax & CTmin will be determined
#'
#' @return A datset of three  columns: "ID" for individual,  "trait" with trait ids and "value" for each trait.
#'
#' @examples
#'
#'@export

get_pop_tpts <- function(TPD, Degree, Pmin){

  # grouping original data and nesting it
  D <- TPD %>% group_by(ID) %>% nest()

  # extract first individiual tpts
  ind_tpdata <- as.data.frame(D$data[1]) # extract individual tpd
  ind_tpts <- get_tpts( Tm = ind_tpdata$Tm, P = ind_tpdata$P, Degree = Degree, Pmin = Pmin) # get individuals tpts
  ind_id <- rep(as.character(D$ID[1]), nrow(ind_tpts)) # get individual ID repeated for as many tpts as you have
  ind <- cbind(ind_id, ind_tpts) # join together
  ind <- rename(ind, ID = ind_id) # renaming ID variable
  pop_tpts <- ind # establish base to rbind next individuals

  # extract population's tpts
  for (i in 2:nrow(D)){

    ind_tpdata <- as.data.frame(D$data[i]) # extract individual tpd
    ind_tpts <- get_tpts( Tm = ind_tpdata$Tm, P = ind_tpdata$P, Degree = Degree, Pmin = Pmin) # get individuals tpts
    ind_id <- rep(as.character(D$ID[i]), nrow(ind_tpts)) # get individual ID repeated for as many tpts as you have
    ind <- cbind(ind_id, ind_tpts) # join together
    ind <- rename(ind, ID = ind_id) # renaming ID variable
    pop_tpts <- rbind(pop_tpts,ind) # rbind each individuals data to pop data

  }

  return(pop_tpts)

}
ggcostoya/tpcurves2 documentation built on Jan. 1, 2021, 2:19 a.m.