R/twoCol.R

#' Prepares data frame for analysis by binomial glm using cbind().
#'
#' \code{twoCol(DF,dropcol)}
#'
#' @param DF a data frame which includes a column of frequencies and
#' at least one column with a binary variable used to split the frequencies.
#' @param dropcol the number of the column which is a binary variable and
#' is used to split the frequencies, and so is dropped from the new
#' data frame.
#'
#' @return a new data frame with half the number of rows as the original,
#' but two columns for the frequencies.
#'
#' @details Removes bottom half of data frame, and appends frequencies
#'as an extra column.
#'
#' @examples
#' # using the built-in Titanic dataset.
#' # First convert it to a data frame
#' TDF = as.data.frame(Titanic)
#' twoCol(TDF,4)

twoCol = function(DF,dropcol){
  DF=DF[order(DF[,dropcol]),]
  DF2 = DF[,-dropcol]
  DF2 = DF2[1:(nrow(DF)/2),]
  DF2[,ncol(DF)]= tail(DF[ncol(DF)],(nrow(DF)/2))
  ID = colnames(DF)[dropcol]
  colnames(DF2)[ncol(DF2)-1]=
      paste0(ID,as.character(DF[1,dropcol]))
  colnames(DF2)[ncol(DF2)]=
      paste0(ID,as.character(DF[nrow(DF),dropcol]))
  return(DF2)
}
helophilus/ColsTools documentation built on May 30, 2019, 4:03 p.m.