[R] Obtaining midpoints of class intervals produced by cut and table

baptiste auguie baptiste.auguie at googlemail.com
Sun Nov 8 22:29:56 CET 2009


Hi,

Maybe something like this (inspired by ?cut),

cut2num <- function(f){
  labs <- levels(f)
  d <- data.frame(lower = as.numeric( sub("\\((.+),.*", "\\1", labs) ),
                  upper = as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", labs) ))
  d$midpoints <- rowMeans(d)
  d
}

a <- c(1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 7)
cut2num(cut(a, 3))

HTH,

baptiste





2009/11/8 jose romero <jlaurentum at yahoo.com>:
> Hello list:
>
> I am using "cut" and "table" to obtain a frequency table from a numeric sample vector.  The idea is to calculate mean and standard deviation on grouped data.  However, I can't extract the midpoints of the class intervals, which seem to be strings treated as factors.  How do i extract the midpoint?
>
> Thanks,
> jose loreto
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>




More information about the R-help mailing list