At Friday 11:46 AM 8/20/2004, Min-Han Tan wrote:>Hi,
>
>I am working on some microarray data, and have some problems with
>writing iterations.
>
>In essence, the problem is that objects with three dimensions don't
>have rownames and colnames. These colnames and rownames would
>otherwise still be there in 2 dimensional objects.
>
>I need to generate multiple iterations of a 2 means-clustering
>algorithm, and these objects thus probably need 3 dimensions.
What objects are you using that are three dimensional but don't have
dimension names? Ordinary arrays have dimension names, and rownames() and
colnames() extract the names on the first two dimensions:
> x <-
array(1:12,dim=c(2,3,2),dimnames=list(letters[1:2],LETTERS[24:26],letters[20:21]))
> x
, , t
X Y Z
a 1 3 5
b 2 4 6
, , u
X Y Z
a 7 9 11
b 8 10 12
> dimnames(x)[[1]]
[1] "a" "b"
> dimnames(x)[[2]]
[1] "X" "Y" "Z"
> x[,"Y",]
t u
a 3 9
b 4 10
> rownames(x)
[1] "a" "b"
> colnames(x)
[1] "X" "Y" "Z"
>
If you need a convenient way to construct three dimensional objects, you
can use the abind() package, e.g.:
> library(abind) # you will have to install the package from CRAN first
> x1 <- matrix(1:6,nrow=2,dimnames=list(letters[1:2],LETTERS[24:26]))
> x2 <- matrix(7:12,nrow=2,dimnames=list(letters[1:2],LETTERS[24:26]))
> abind(list(t=x1, u=x2), along=3)
, , t
X Y Z
a 1 3 5
b 2 4 6
, , u
X Y Z
a 7 9 11
b 8 10 12
>
(The objects to be bound don't have to be given to abind() in a list, but
this manner of invocation is convenient when one happens to have a list of
objects to be bound together, as one might get in the result from lapply().)
>My scripts are all written with heavy references to matching of
>colnames and rownames, so I am running into some problems here.
>(colnames = sample ids, and rownames = gene ids)
Last time I looked, subscripting matrices and arrays with strings was very
slow (for large objects), so if you are using character subscripts and
you're having problems with slowness, consider doing the indexing yourself
using match(), e.g.:
> x <- matrix(rnorm(26^4), ncol=26,
dimnames=list(paste(rep(letters,each=26^2),rep(letters,each=26),letters,sep=""),
LETTERS))
> dim(x)
[1] 17576 26
> xr <- sample(rownames(x), 10000)
> length(xr)
[1] 10000
> system.time(y <- x[xr, ])
[1] 2.22 0.00 2.30 NA NA
> system.time(y <- x[match(xr, rownames(x)), ])
[1] 0.09 0.00 0.09 NA NA
>
HTH
-- Tony Plate
>My bad workaround solution so far has been to generate objects tagged
>with ".2", and have multiple blocks of code.
>
>e.g.
>
>test.1 <- ...
>
>test.2 <- ...
>
>test.x <- ..
>
>The obvious problem with this solution is that there does not seem to
>be an easy way of manipulating all these objects together without
>typing out their names individually.
>
>Thanks for any advice.
>
>Regards,
>Min-Han
>
>______________________________________________
>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