# Re: [R] how to manipulate counts matrix?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed 18 Oct 2006 - 20:34:08 GMT

It occurred to me that if NAs, exponential notation and Inf is allowed in the intervals then the first two solutions I gave previously would not work. Here it is again such that all three solutions should work:

rn <- c("[-11.9,-10.6]", "(NA,9.3]", "(9.3,8e01]", "(8.01,Inf]")

# 1
library(gsubfn)
colMeans(strapply(rn, "[^\][(),]+", as.numeric, simplify = TRUE))

# 2
library(gsubfn)
strapply(rn, "^.([^,]+), *(.+).\$",
~ (as.numeric(x) + as.numeric(y))/2, backref = -2, simplify = TRUE)

# 3
with(read.table(textConnection(chartr("[](),", " ", rn))), (V1+V2)/2)

On 10/18/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> Here are three solutions:
>
>
> # Assume rn holds the rownames of the table
> # rn <- rownames(my.table)
>
> rn <- c("[-11.9,-10.6]", "(-10.6,-9.3]", "(-9.3,-8.01]", "(-8.01,-6.73]")
>
> # 1
> library(gsubfn)
> colMeans(strapply(rn, "[-.0-9]+", as.numeric, simplify = TRUE))
>
> # 2
> library(gsubfn)
> strapply(rn, "([-.0-9]+), *([-.0-9]+)",
> ~ (as.numeric(x) + as.numeric(y))/2, backref = -2, simplify = TRUE)
>
> # 3
> with(read.table(textConnection(chartr("[](),", " ", rn))), (V1+V2)/2)
>
>
> The gsubfn demo
>
> library(gsubfn)
> demo("gsubfn-cut")
>
> shows a similar problem to his one.
>
>
> On 10/18/06, Jenny Stadt <jennystadt@yahoo.ca> wrote:
> > Hi List,
> >
> > I've got a count matrix like this:
> >
> >
> > [-11.9,-11] (-11,-10.2] (-10.2,-9.3] (-9.3,-8.44]
> > [-11.9,-10.6] 0 0 0 0
> > (-10.6,-9.3] 0 0 0 0
> > (-9.3,-8.01] 0 0 0 0
> > (-8.01,-6.73] 0 0 0 0
> >
> > I want to manipulate the matrix by taking the average of each range, eg. [-11.9,-11] as (-11.9-11)/2. I've tried R introduction and site search, but not able to find information relevant.
> >
> > Thanks!
> >
> > Jen
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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. Received on Thu Oct 19 06:51:31 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 18 Oct 2006 - 21:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.