Obtaining midpoints of class intervals produced by cut and table

4 messages Options
Embed this post
Permalink
jose romero-3

Obtaining midpoints of class intervals produced by cut and table

Reply Threaded More More options
Print post
Permalink
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]]


______________________________________________
[hidden email] 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.
baptiste auguie-5

Re: Obtaining midpoints of class intervals produced by cut and table

Reply Threaded More More options
Print post
Permalink
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 <[hidden email]>:

> 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]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.
Peter Dalgaard

Re: Obtaining midpoints of class intervals produced by cut and table

Reply Threaded More More options
Print post
Permalink
In reply to this post by jose romero-3
jose romero wrote:
> 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?
>

You might consider starting with hist() instead. Otherwise, how about

mid <- brk[-1] - diff(brk)/2

where brk is the breakpoints used in cut()

--
    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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.
Gabor Grothendieck

Re: Obtaining midpoints of class intervals produced by cut and table

Reply Threaded More More options
Print post
Permalink
In reply to this post by jose romero-3
Try this:

library(gsubfn)
demo("gsubfn-cut")

and note the strapply call that converts the levels from cut to a matrix.

On Sun, Nov 8, 2009 at 4:08 PM, jose romero <[hidden email]> wrote:

> 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]]
>
>
> ______________________________________________
> [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.