[R] how to manipulate counts matrix?

Gabor Grothendieck ggrothendieck at gmail.com
Wed Oct 18 22:34:08 CEST 2006


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 at 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 at 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 at 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.
> >
>



More information about the R-help mailing list