Skip to content
Prev 70387 / 398525 Next

Contingency tables from data.frames

Gabor Grothendieck wrote:
Thanks Gabor, you suggestion solve my basic problem.
I'm working is same basic (but I think useful) functions
for begginiers.

Below you can see the set of functions:

#######################
#   EasyeR - Package  #
#######################

# Common function---------------------------------------------------------------
er.make.table <- function(x,
                           classIni,
                           classEnd,
                           h,
                           right) {
   # Absolut frequency
   f <- table(cut(x, br = seq(classIni, classEnd, h), right = right))

   # Relative frequency
   fr <- f/length(x)

   # Relative frequency, %
   frP <- 100*(f/length(x))

   # Cumulative frequency
   fac <- cumsum(f)

   # Cumulative frequency, %
   facP <<- 100*(cumsum(f/length(x)))

   fi   <- round(f, 2)
   fr   <- round(as.numeric(fr), 2)
   frP  <- round(as.numeric(frP), 2)
   fac  <- round(as.numeric(fac), 2)
   facP <- round(as.numeric(facP),2)

   # Table
   res <- data.frame(fi, fr, frP, fac, facP)
   names(res) <- c('Class limits', 'fi', 'fr', 'fr(%)', 'fac', 'fac(%)')
   return(res)
}


#1. Tables from vectors
#1.1---er.table.br (User define breaks and right)-------------------------------
er.table.br <- function(x,
                         breaks = c('Sturges', 'Scott', 'FD'),
                         right = FALSE) {

   if (is.factor(x) || mode(x) != 'numeric') stop('need numeric data')

   x <- na.omit(x)

   k <- switch(breaks[1],
               'Sturges' = nclass.Sturges(x),
               'Scott'   = nclass.scott(x),
               'FD'      = nclass.FD(x),
               stop("'breaks' must be 'Sturges', 'Scott' or 'FD'"))

   tmp      <- range(x)
   classIni <- tmp[1] - abs(tmp[2])/100
   classEnd <- tmp[2] + abs(tmp[2])/100
   R        <- classEnd-classIni
   h        <- R/k

   tbl <- er.make.table(x, classIni, classEnd, h, right)
   return(tbl)
}


#1.2---er.table.kr (User define the class number (k) and right)-----------------
er.table.kr <- function(x,
                         k,
                         right = FALSE) {

   if (is.factor(x) || mode(x) != 'numeric') stop('need numeric data')
   if ((k == '') || (k == ' ')) stop('k not defined')

   x <- na.omit(x)

   tmp      <- range(x)
   classIni <- tmp[1] - abs(tmp[2])/100
   classEnd <- tmp[2] + abs(tmp[2])/100
   R        <- classEnd-classIni
   h        <- R/k

   tbl <- er.make.table(x, classIni, classEnd, h, right)
   return(tbl)
}

#1.3---er.table.ier (User define the classIni, classEnd and right)--------------
er.table.ier <- function(x,
                          classIni,
                          classEnd,
                          right = FALSE) {

   if (is.factor(x) || mode(x) != 'numeric') stop('need numeric data')
   if ((classIni == '') || (classIni == ' ')) stop('classIni not defined')
   if ((classEnd == '') || (classEnd == ' ')) stop('classEnd not defined')

   x <- na.omit(x)

   tmp <- range(x)
   R   <- classEnd-classIni
   k   <- sqrt(abs(R))
   if (k < 5)  k <- 5
   h   <- R/k

   tbl <- er.make.table(x, classIni, classEnd, h, right)
   return(tbl)
}


#1.4---er.table.all (User define classIni, ClassEnd, h and right)---------------
er.table.iehr <- function(x,
                          classIni,
                          classEnd,
                          h,
                          right=FALSE) {

   if (is.factor(x) || mode(x) != 'numeric') stop('need numeric data')
   if ((classIni == '') || (classIni == ' ')) stop('classIni not defined')
   if ((classEnd == '') || (classEnd == ' ')) stop('classEnd not defined')
   if ((h == '')        || (h == ' '))        stop('h not defined')

   x <- na.omit(x)

   tbl <- er.make.table(x, classIni, classEnd, h, right)
   return(tbl)
}

#2. Tables from data.frames
#2.1---er.table.df.br (User define breaks and right)----------------------------
er.table.df.br <- function(df,
                            breaks = c('Sturges', 'Scott', 'FD'),
                            right = FALSE) {

   tmpList <- list()

   if (is.data.frame(df) != 'TRUE') stop('need "data.frame" data')

   logCol  <- sapply(df, is.numeric)
   dim_df  <- dim(df)

   for (i in 1:dim_df[2]) {
     if (logCol[i]!=FALSE) {
       x <- as.matrix(df[ ,i])
       x <- na.omit(x)

       k <- switch(breaks[1],
                   'Sturges' = nclass.Sturges(x),
                   'Scott'   = nclass.scott(x),
                   'FD'      = nclass.FD(x),
                   stop("'breaks' must be 'Sturges', 'Scott' or 'FD'"))

       tmp      <- range(x)
       classIni <- tmp[1] - abs(tmp[2])/100
       classEnd <- tmp[2] + abs(tmp[2])/100
       R        <- classEnd-classIni
       h        <- R/k

       tbl <- er.make.table(x, classIni, classEnd, h, right)
       tmpList <- c(tmpList, list(tbl))
     }
   }
   valCol <- logCol[logCol!=FALSE]
   names(tmpList) <- names(valCol)
   return(tmpList)
}


#2.2---er.table.df.kr (User define the class number (k) and right)--------------
er.table.df.kr <- function(df,
                            k,
                            right = FALSE) {

   if ((k == '') || (k == ' ')) stop('k not defined')
   if (is.data.frame(df) != 'TRUE') stop('need "data.frame" data')

   tmpList <- list()

   logCol  <- sapply(df, is.numeric)
   dim_df  <- dim(df)

   for (i in 1:dim_df[2]) {
     if (logCol[i]!=FALSE) {
       x <- as.matrix(df[ ,i])
       x <- na.omit(x)

       tmp      <- range(x)
       classIni <- tmp[1] - abs(tmp[2])/100
       classEnd <- tmp[2] + abs(tmp[2])/100
       R        <- classEnd-classIni
       h        <- R/k

       tbl <- er.make.table(x, classIni, classEnd, h, right)
       tmpList <- c(tmpList, list(tbl))
     }
   }
   valCol <- logCol[logCol!=FALSE]
   names(tmpList) <- names(valCol)
   return(tmpList)
}

Best regards,