[R] intervals from cut() as numerics?

Berwin A Turlach berwin at maths.uwa.edu.au
Sat May 20 11:39:57 CEST 2006


G'day Gavin,

>>>>> "GS" == Gavin Simpson <gavin.simpson at ucl.ac.uk> writes:

    GS> The problem is getting the range/interval for each group from
    GS> (4,4.3], so I can automate this.
Most likely there is an easier way, but this seems to work:

## get the levels of groups:
> tmp <- levels(groups)
## remove the opening "(" and closing "]" from the string:
> tmp1 <- sapply(tmp, function(x) substr(x, 2, nchar(x)-1))
## split into two character strings:
> tmp2 <- strsplit(tmp1, ",")
## turn into results into two numbers:
> tmp3 <- lapply(tmp2, as.numeric)

## Of course, we can do everything in one go:
> lapply(strsplit(sapply(levels(groups), function(x) substr(x, 2, nchar(x)-1)), ","), as.numeric)
$"(4.05,4.32]"
[1] 4.05 4.32

$"(4.32,4.6]"
[1] 4.32 4.60

$"(4.6,4.87]"
[1] 4.60 4.87

$"(4.87,5.15]"
[1] 4.87 5.15

$"(5.15,5.43]"
[1] 5.15 5.43

$"(5.43,5.7]"
[1] 5.43 5.70

$"(5.7,5.98]"
[1] 5.70 5.98

$"(5.98,6.25]"
[1] 5.98 6.25

$"(6.25,6.53]"
[1] 6.25 6.53

$"(6.53,6.8]"
[1] 6.53 6.80

Cheers,

        Berwin

========================== Full address ============================
Berwin A Turlach                      Tel.: +61 (8) 6488 3338 (secr)   
School of Mathematics and Statistics        +61 (8) 6488 3383 (self)      
The University of Western Australia   FAX : +61 (8) 6488 1028
35 Stirling Highway                   
Crawley WA 6009                e-mail: berwin at maths.uwa.edu.au
Australia                        http://www.maths.uwa.edu.au/~berwin




More information about the R-help mailing list