R/aggregate_by_unique_in_a_column_v1.R

Defines functions aggregate_by_unique_in_a_column_v1

Documented in aggregate_by_unique_in_a_column_v1

#' Aggregate by unique cases in column 1 (version 1)
#' 
#' Version 1 aggregates by summing the numbers. Make sure the column on which agreegation is based is first from the left.
#' 
#' @param df Input data frame to be aggregated.
#' @return Aggregated df
#' @export 
#' 
aggregate_by_unique_in_a_column_v1 = function(df){
  df[,col] = as.character(df[,1])
  u = unique(df[,1])
  o = as.data.frame(matrix(nrow=1,ncol=ncol(df)-1))
  for(i in 1:length(u)){
    #print(paste("Processing ", u[i],sep = ""))
    case = df[df[,1]==u[i],2:ncol(df)]
    case = colSums(case)
    o = rbind(o,case)
  }
  colnames(o) = colnames(df[,-1])
  o = o[-1,]
  rownames(o) = u
  o
}
msxakk89/dat documentation built on Aug. 3, 2020, 6:39 p.m.