jose romero
2009-Nov-08 21:08 UTC
[R] Obtaining midpoints of class intervals produced by cut and table
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]]
baptiste auguie
2009-Nov-08 21:29 UTC
[R] Obtaining midpoints of class intervals produced by cut and table
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. > >
Peter Dalgaard
2009-Nov-08 21:31 UTC
[R] Obtaining midpoints of class intervals produced by cut and table
jose romero wrote:> Hello list: > > I am using "cut" and "table" to obtain a frequency table from anumeric 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 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Gabor Grothendieck
2009-Nov-08 21:45 UTC
[R] Obtaining midpoints of class intervals produced by cut and table
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 <jlaurentum at yahoo.com> 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]] > > > ______________________________________________ > 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. > >
Maybe Matching Threads
- Integrate a 1-variable function with 1 parameter (Jose L. Romero)
- Cut intervals (character) to numeric midpoint; regex problem
- problem with switch function across R versions 2.10 and 2.13
- summary statistics for grouped data
- Vectorization of duration of the game in the gambler ruin's problem