[R] levels values of cut()

2008-08-09 Thread baptiste auguie

Dear list,

 I have the following example, from which I am hoping to retrieve  
numeric values of the factor levels (that is, without the brackets):




x <- seq(1, 15, length=100)
y <- sin(x)

my.cuts <- cut(which(abs(y) < 1e-1), 3)
levels(my.cuts)


hist() does not suit me for this, as it does not necessarily respect  
the number of breaks.


getAnywhere hasn't got me very far: I cannot seem to find a readable  
code for the built-in cut function in the base library. I think  
getMethod should do it but I don't understand the arguments to pass.


Any pointers appreciated,

Thanks,

baptiste



_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
R-help@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.


Re: [R] levels values of cut()

2008-08-09 Thread Stephen Tucker
Not sure what you're looking for, but does this help?

Extending your code,

> library(gsubfn)
> t(strapply(levels(my.cuts),"([0-9.]+),([0-9.]+)",
+  function(...) as.numeric(c(...)),backref=-2,simplify=TRUE))
 [,1] [,2]
[1,] 15.9 38.3
[2,] 38.3 60.7
[3,] 60.7 83.1



- Original Message 
From: baptiste auguie <[EMAIL PROTECTED]>
To: r-help@r-project.org
Sent: Saturday, August 9, 2008 1:51:01 AM
Subject: [R] levels values of cut()

Dear list,

  I have the following example, from which I am hoping to retrieve  
numeric values of the factor levels (that is, without the brackets):

>
> x <- seq(1, 15, length=100)
> y <- sin(x)
>
> my.cuts <- cut(which(abs(y) < 1e-1), 3)
> levels(my.cuts)

hist() does not suit me for this, as it does not necessarily respect  
the number of breaks.

getAnywhere hasn't got me very far: I cannot seem to find a readable  
code for the built-in cut function in the base library. I think  
getMethod should do it but I don't understand the arguments to pass.

Any pointers appreciated,

Thanks,

baptiste



_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
R-help@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.





__
R-help@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.


Re: [R] levels values of cut()

2008-08-09 Thread Prof Brian Ripley

On Sat, 9 Aug 2008, baptiste auguie wrote:


Dear list,

I have the following example, from which I am hoping to retrieve numeric 
values of the factor levels (that is, without the brackets):




x <- seq(1, 15, length=100)
y <- sin(x)

my.cuts <- cut(which(abs(y) < 1e-1), 3)
levels(my.cuts)


hist() does not suit me for this, as it does not necessarily respect the 
number of breaks.


getAnywhere hasn't got me very far: I cannot seem to find a readable code for 
the built-in cut function in the base library. I think getMethod should do it 
but I don't understand the arguments to pass.


Not getMethod (that's for S4 methods).  Just type cut.default at the R 
prompt.


However, try

example(cut)
foo <- levels(cut(aaa, 3))
lims <- matrix(nrow=length(foo), ncol=2)
lims[,1] <- as.numeric( sub("\\((.+),.*", "\\1", foo) )
lims[,2] <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", foo) )

--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

__
R-help@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.


Re: [R] levels values of cut()

2008-08-09 Thread baptiste auguie
Thank you all for the precious tips. For memory I've made the  
following wrapper function for this. I wonder whether a short note on  
these regular expressions could be useful on the help page of cut().




cutIntervals <- function(x, ...){
dotArgs <- unlist(c(...))
	if( any(names(dotArgs) == "labels")) stop("labels cannot be  
specified,  use cut instead")


cut.fact <- levels(cut(x,labels=NULL, ...))
# tip from Brian Ripley
lims <- matrix(nrow=length(cut.fact), ncol=2)
lims[,1] <- as.numeric( sub("\\((.+),.*", "\\1", cut.fact) )
lims[,2] <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", cut.fact) )
# alternatively (Stephen Tucker)
 # library(gsubfn)
 # lims <- t(strapply(cut.fact,"([0-9.]+),([0-9.]+)",
 #  function(...) 
as.numeric(c(...)),backref=-2,simplify=TRUE))
lims
}

cutIntervals(1:5, 3)



Many thanks,

baptiste

On 9 Aug 2008, at 11:12, Prof Brian Ripley wrote:


On Sat, 9 Aug 2008, baptiste auguie wrote:


Dear list,

I have the following example, from which I am hoping to retrieve  
numeric values of the factor levels (that is, without the brackets):



x <- seq(1, 15, length=100)
y <- sin(x)
my.cuts <- cut(which(abs(y) < 1e-1), 3)
levels(my.cuts)


hist() does not suit me for this, as it does not necessarily  
respect the number of breaks.


getAnywhere hasn't got me very far: I cannot seem to find a  
readable code for the built-in cut function in the base library. I  
think getMethod should do it but I don't understand the arguments  
to pass.


Not getMethod (that's for S4 methods).  Just type cut.default at the  
R prompt.


However, try

example(cut)
foo <- levels(cut(aaa, 3))
lims <- matrix(nrow=length(foo), ncol=2)
lims[,1] <- as.numeric( sub("\\((.+),.*", "\\1", foo) )
lims[,2] <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", foo) )

--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595


_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
R-help@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.


Re: [R] levels values of cut()

2008-08-09 Thread Prof Brian Ripley

On Sat, 9 Aug 2008, baptiste auguie wrote:

Thank you all for the precious tips. For memory I've made the following 
wrapper function for this. I wonder whether a short note on these regular 
expressions could be useful on the help page of cut().


Already there in R-devel 





cutIntervals <- function(x, ...){
dotArgs <- unlist(c(...))
	if( any(names(dotArgs) == "labels")) stop("labels cannot be 
specified,  use cut instead")


cut.fact <- levels(cut(x,labels=NULL, ...))
# tip from Brian Ripley
lims <- matrix(nrow=length(cut.fact), ncol=2)
lims[,1] <- as.numeric( sub("\\((.+),.*", "\\1", cut.fact) )
lims[,2] <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", cut.fact) )
# alternatively (Stephen Tucker)
 # library(gsubfn)
 # lims <- t(strapply(cut.fact,"([0-9.]+),([0-9.]+)",
	 # 		function(...) 
as.numeric(c(...)),backref=-2,simplify=TRUE))

lims
}

cutIntervals(1:5, 3)



Many thanks,

baptiste

On 9 Aug 2008, at 11:12, Prof Brian Ripley wrote:


On Sat, 9 Aug 2008, baptiste auguie wrote:


Dear list,

I have the following example, from which I am hoping to retrieve numeric 
values of the factor levels (that is, without the brackets):



x <- seq(1, 15, length=100)
y <- sin(x)
my.cuts <- cut(which(abs(y) < 1e-1), 3)
levels(my.cuts)


hist() does not suit me for this, as it does not necessarily respect the 
number of breaks.


getAnywhere hasn't got me very far: I cannot seem to find a readable code 
for the built-in cut function in the base library. I think getMethod 
should do it but I don't understand the arguments to pass.


Not getMethod (that's for S4 methods).  Just type cut.default at the R 
prompt.


However, try

example(cut)
foo <- levels(cut(aaa, 3))
lims <- matrix(nrow=length(foo), ncol=2)
lims[,1] <- as.numeric( sub("\\((.+),.*", "\\1", foo) )
lims[,2] <- as.numeric( sub("[^,]*,([^]]*)\\]", "\\1", foo) )

--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595


_

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

__
R-help@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.


--
Brian D. Ripley,  [EMAIL PROTECTED]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595__
R-help@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.