[R] Frequency table

joseclaudio.faria joseclaudio.faria at terra.com.br
Fri Mar 19 04:09:19 CET 2004


Hi,

See if this generic function I made can help you.

data <- c(65, 70, 85, 65, 65, 65, 62, 55, 82, 59,
               55, 66, 74, 55, 65, 56, 80, 73, 45, 64,
               75, 58, 60, 56, 60, 65, 53, 63, 72, 80,
               90, 95, 55, 70, 79, 62, 57, 65, 60, 47,
               61, 53, 80, 75, 72, 87, 52, 72, 80, 85,
               75, 70, 84, 60, 72, 70, 76, 70, 79, 72,
               69, 80, 62, 74, 54, 58, 58, 69, 81, 84)

#------------ begin options of table---------------
min <-    40
max <- 100
h   <-     10
#-------------- end options of table---------------

#-------- begin declaration of variables-----------
Fi <- numeric(); FacA <- numeric(); FacP <- numeric(); FrA   <- numeric();
FrP    <- numeric()
#-------- end declaration of variables-------------

#----------------- begin function------------------
Createtable <- function()
{
  Fi <<- table(cut(data, br = seq(min, max, h), right = FALSE))
  K <- length(names(Fi))
  n <- length(data)

  for(i in 1:K)
  {
    FrA[i] = Fi[i] / n
  }

  for(i in 1:K)
  {
    FrP[i] = (Fi[i] / n) * 100
  }

  for(i in 1:K)
  {
    FacA[i] = sum(Fi[1:i])
  }

  for(i in 1:K)
  {
    FacP[i] = (sum(Fi[1:i]) / n) * 100
  }

  table <- data.frame(Fi, FrA, FrP, FacA, FacP)
}
#----------------- end function------------------

tab <- Createtable()
print("Complete table:")
print(tab)

José Cláudio Faria
UESC/DCET
Brasil
73-634.2779
joseclaudio.faria at terra.com.br
jc_faria at uol.com.br




More information about the R-help mailing list