R/latextab.R

Defines functions latextab

#' Function for creating latex tables from mrm results
#'
#' @ indat = matrix containing community data
#' @ plotno = character vector indicating column name for altitude values
#' @ group =character vector indicating column name for site IDs (if there is more than one site )
#'
latextab<-function(indat){
  # convert variables into characters
  indat %>%
    mutate(variable=as.character(variable)) %>%
    mutate(scale=as.numeric(as.character(scale)))  %>%
    # Slopes
    # p-value codes p.coef > 0.01  & p.coef < 0.05
    mutate(p.coef1 = ifelse(p.coef > 0.01  & p.coef < 0.05,"*","")) %>%
    # p-value codes p.coef > 0.001  & p.coef < 0.01
    mutate(p.coef1 = ifelse(p.coef > 0.001  & p.coef < 0.01,"**",p.coef1)) %>%
    # p-value codes p.coef <  0.001
    mutate(p.coef1 = ifelse(p.coef < 0.001,"***",p.coef1)) %>%
    # coefficient + p.value
    mutate(coef1 = paste(round(coef,digits=3),p.coef1))  %>%
    # substitute two NAs with one NA
    mutate(coef1=ifelse(coef1=="NA NA",NA,coef1))  %>%
    # R2
    # p-value codes p.coef > 0.01  & p.coef < 0.05
    mutate(R2.pvalue1 = ifelse(R2.pvalue > 0.01  & R2.pvalue < 0.05,"*","")) %>%
    # p-value codes p.coef > 0.001  & p.coef < 0.01
    mutate(R2.pvalue1 = ifelse(R2.pvalue > 0.001  & R2.pvalue < 0.01,"**",R2.pvalue1)) %>%
    # p-value codes p.coef <  0.001
    mutate(R2.pvalue1 = ifelse(R2.pvalue < 0.001,"***",R2.pvalue1)) %>%
    # coefficient + p.value
    mutate(R21 = paste(round(R2,digits=2),R2.pvalue1))  %>%
    # substitute two NAs with one NA
    mutate(R21=ifelse(R21=="NA NA",NA,R21)) %>%
    # create final dataframe
    select(Island,habitat,comp,coef1,scale,variable,R21) %>%
    # reshape dataframe
    gather(key=qinterest,value=value,-c(habitat,comp,variable,Island,scale)) %>%
    # create new column names
    mutate(colname=ifelse(qinterest=="coef1",paste(comp,"_coef",sep=""),paste(comp,"_R2",sep="")))  %>%
    select(habitat,variable,Island,scale,value,colname)  %>%
    spread(colname,value) %>%
    arrange(Island,scale) ->results
  # convert into latex table
  results1<-kable(results, format = "latex", booktabs = T,longtable = T) %>%
    kable_styling() %>%
    add_header_above(c(" " = 3, "Brepl" = 2,"Brich" = 2,"Btot" = 2))
  return(results1)
}
marcog77/Rmacdiv documentation built on March 20, 2018, 5:25 a.m.