R/propOfCategoryPerSideOfPlane.R

Defines functions propOfCategoryPerSideOfPlane

Documented in propOfCategoryPerSideOfPlane

#' Calculare proportion of dots belonging to category at each side of Cartesian plane
#' 
#' @param PC1_PC2_df A data frame where PC1_PC2_df[,1] is a redundant character vector of categories corresponding to points on plane, PC1_PC2_df[,2] is a numeric vector of PC1, PC1_PC2_df[,2] is a numeric vector of PC2.
#' @return A data frame of counts per plane side (first 4 columns), total counts (5th column), and proportions per total of each category (last 4 columns).
#' @export

propOfCategoryPerSideOfPlane <- function(PC1_PC2_df){
  # initializing a vector of total counts
  totalCounts <-c()
  # appending a vector of total counts per category
  categories <- unique(PC1_PC2_df[,1])
  for(i in 1:length(categories)){
    current_i <- countOfsomething.2(PC1_PC2_df[,1], categories[i])
    totalCounts <- c(totalCounts, current_i)
  }
  # initializing output data
  o <- as.data.frame(matrix(nrow = 1, ncol = 9))
  colnames(o) <- c("Cartesian plane 1 count", "Cartesian plane 2 count", "Cartesian plane 3 count", "Cartesian plane 4 count", "Total", "Cartesian plane 1 prop", "Cartesian plane 2 prop", "Cartesian plane 3 prop", "Cartesian plane 4 prop")
  # counting category instance at each each side of Cartesian plane
  #         |
  #     2   |   1  
  #     ____|_____
  #         |
  #     3   |   4
  #         |
  # Assuming PC1_PC2_df[,2] is for PC1 while PC1_PC2_df[,3] is for PC2
  for(i in 1:length(categories)){
    PC1_PC2_df_perCategory <- PC1_PC2_df[PC1_PC2_df[,1]==categories[i],]
    in1 <- nrow(PC1_PC2_df_perCategory[PC1_PC2_df_perCategory[,2] > 0 & PC1_PC2_df_perCategory[,3] > 0,])
    in2 <- nrow(PC1_PC2_df_perCategory[PC1_PC2_df_perCategory[,2] < 0 & PC1_PC2_df_perCategory[,3] > 0,])
    in3 <- nrow(PC1_PC2_df_perCategory[PC1_PC2_df_perCategory[,2] < 0 & PC1_PC2_df_perCategory[,3] < 0,])
    in4 <- nrow(PC1_PC2_df_perCategory[PC1_PC2_df_perCategory[,2] > 0 & PC1_PC2_df_perCategory[,3] < 0,])
    
    if(!(sum(in1,in2,in3,in4)==totalCounts[i])){ # sanity check
      stop("Counts per plane side do not add up to the total!")
    }
    # calc proportions
    prop1 <- in1 / totalCounts[i]
    prop2 <- in2 / totalCounts[i]
    prop3 <- in3 / totalCounts[i]
    prop4 <- in4 / totalCounts[i]
    
    # attaching data to output
    o <- rbind(o, c(in1,in2,in3,in4,totalCounts[i],prop1,prop2,prop3,prop4))
  }
  o <- o[-1,]
  row.names(o) <- categories
  o
} 
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.