Dear list, If anyone knows how to assign dimnames to matrices or arrays I would be most grateful for help. I've tried various permutations of likely-looking code but get error messages every time. I could find no example in the documentation. Many thanks, Dan Bebber Department of Plant Sciences University of Oxford South Parks Road Oxford OX1 3RB UK Tel. 01865 275000
I believe the posting guide ask you to show an example of what you tried,
just so people have a better idea of where you went off track.
I thought ?dimnames is rather explicit on this:
[...]
Usage:
dimnames(x)
dimnames(x) <- value
Arguments:
x: an R object, for example a matrix, array or data frame.
value: a possible value for 'dimnames(x)': see "Value".
[...]
Value:
The dimnames of a matrix or array can be 'NULL' or a list of the
same length as 'dim(x)'. If a list, its components are either
'NULL' or a character vector the length of the appropriate
dimension of 'x'.
Thus, something like:
> x <- matrix(1:12, 3, 4)
> x
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12> dimnames(x) <- list(letters[1:nrow(x)], LETTERS[1:ncol(x)])
> x
A B C D
a 1 4 7 10
b 2 5 8 11
c 3 6 9 12
Andy
> From: Dan Bebber
>
> Dear list,
>
> If anyone knows how to assign dimnames to matrices or arrays
> I would be most
> grateful for help. I've tried various permutations of
> likely-looking code
> but get error messages every time. I could find no example in the
> documentation.
>
> Many thanks,
> Dan Bebber
>
> Department of Plant Sciences
> University of Oxford
> South Parks Road
> Oxford OX1 3RB
> UK
> Tel. 01865 275000
>
> ______________________________________________
> 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
>
>
Take a look at ?"dimnames<-" Note the quotes. -roger Dan Bebber wrote:> Dear list, > > If anyone knows how to assign dimnames to matrices or arrays I would be most > grateful for help. I've tried various permutations of likely-looking code > but get error messages every time. I could find no example in the > documentation. > > Many thanks, > Dan Bebber > > Department of Plant Sciences > University of Oxford > South Parks Road > Oxford OX1 3RB > UK > Tel. 01865 275000 > > ______________________________________________ > 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 >
Hi Dan,
try this:
?dimnames
mat <- matrix(rnorm(10*3), 10, 3)
dimnames(mat) <- list(letters[1:10], c("X", "Y",
"Z"))
mat
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat/
http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Dan Bebber" <danbebber at forestecology.co.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, September 27, 2004 4:46 PM
Subject: [R] cannot assign dimnames
> Dear list,
>
> If anyone knows how to assign dimnames to matrices or arrays I would
> be most
> grateful for help. I've tried various permutations of likely-looking
> code
> but get error messages every time. I could find no example in the
> documentation.
>
> Many thanks,
> Dan Bebber
>
> Department of Plant Sciences
> University of Oxford
> South Parks Road
> Oxford OX1 3RB
> UK
> Tel. 01865 275000
>
> ______________________________________________
> 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
>
Dear Dan, a simple example:> mat<-matrix(1:6, ncol=3) > mat[,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6> dimnames(mat)<-list(1:2, 1:3) > mat1 2 3 1 1 3 5 2 2 4 6> dimnames(mat)<-list(1:2, letters[1:3]) > mata b c 1 1 3 5 2 2 4 6> dimnames(mat)[[1]] [1] "1" "2" [[2]] [1] "a" "b" "c" dimnames need a list! HIH jan On Mon, 27 Sep 2004, Dan Bebber wrote:> Dear list, > > If anyone knows how to assign dimnames to matrices or arrays I would be most > grateful for help. I've tried various permutations of likely-looking code > but get error messages every time. I could find no example in the > documentation. > > Many thanks, > Dan Bebber > > Department of Plant Sciences > University of Oxford > South Parks Road > Oxford OX1 3RB > UK > Tel. 01865 275000 > > ______________________________________________ > 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-- +----------------------------------------- Jan Goebel j g o e b e l @ d i w . d e DIW Berlin German Socio-Economic Panel Study (GSOEP) K??nigin-Luise-Str. 5 D-14195 Berlin -- Germany -- phone: 49 30 89789-377 +-----------------------------------------
Dear list, Please ignore my earlier message on this topic. I was under the mistaken impression that dimnames() named the dimensions themselves, rather than the indices within the dimensions. Many thanks, Dan Bebber
Dan Bebber <danbebber <at> forestecology.co.uk> writes: : If anyone knows how to assign dimnames to matrices or arrays I would be most : grateful for help. I've tried various permutations of likely-looking code : but get error messages every time. I could find no example in the : documentation. Here are 4 ways of assigning dimnames to a matrix. # matrix - 1 mat1 <- matrix(1:12,4,3, dimnames = list(letters[1:4], LETTERS[1:3])) # matrix - 2 mat2 <- matrix(1:12,4,3) dimnames(mat2) <- list(letters[1:4], LETTERS[1:3]) # matrix - 3 mat3 <- matrix(1:12,4,3) attr(mat3, "dimnames") <- list(letters[1:4], LETTERS[1:3]) # matrix - 4 mat4 <- matrix(1:12,4,3) rownames(mat4) <- letters[1:4] colnames(mat4) <- LETTERS[1:3] # For arrays its similar, e.g. here is #1 redone for an array: arr <- array(1:24, c(2,3,4),dimnames=list(letters[1:2],LETTERS[1:3],month.abb [1:4])) # For a data frame the various forms above also work except that # (a) this form is also available DF <- data.frame(A = 1:4, B = 5:8, C = 9:12, row.names = letters[1:4]) # and (b) for data frames one must use row.names in place of # rownames though colnames still works as does names (which works # like colnames)
Dan Bebber <danbebber <at> forestecology.co.uk> writes:
: Please ignore my earlier message on this topic. I was under the mistaken
: impression that dimnames() named the dimensions themselves, rather than the
: indices within the dimensions.
What you are referring to is done in R by referring to the names of
the dimnames, as opposed to the dimnames, themselves. Here is an
example:
R> mat <- matrix(1:12,4, dimnames = list(letters[1:4],LETTERS[1:3]))
R> names(dimnames(mat)) <- c("FirstDim", "SecondDim")
R> mat
SecondDim
FirstDim A B C
a 1 5 9
b 2 6 10
c 3 7 11
d 4 8 12
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:
:
: Dan Bebber <danbebber <at> forestecology.co.uk> writes:
:
: : Please ignore my earlier message on this topic. I was under the mistaken
: : impression that dimnames() named the dimensions themselves, rather than the
: : indices within the dimensions.
:
: What you are referring to is done in R by referring to the names of
: the dimnames, as opposed to the dimnames, themselves. Here is an
: example:
:
: R> mat <- matrix(1:12,4, dimnames = list(letters[1:4],LETTERS[1:3]))
: R> names(dimnames(mat)) <- c("FirstDim",
"SecondDim")
: R> mat
: SecondDim
: FirstDim A B C
: a 1 5 9
: b 2 6 10
: c 3 7 11
: d 4 8 12
Perhaps I should have also mentioned that the above could be done
in a single line like this:
R> mat <- matrix(1:12,4, dimnames = list(FirstDim = letters[1:4],
SecondDim =
LETTERS[1:3]))
R> mat
SecondDim
FirstDim A B C
a 1 5 9
b 2 6 10
c 3 7 11
d 4 8 12