Re: [R] name ONLY one column // empty.dimnames() in 'sfsmisc'

2010-09-29 Thread Martin Maechler
 IC == Ivan Calandra ivan.calan...@uni-hamburg.de
 on Mon, 27 Sep 2010 16:59:31 +0200 writes:

IC Hi,
IC I'm not sure it's even possible (and if it is I don't know how, but I'm 
IC no expert).

yes it is possible (in some way), see below.

IC But I think it doesn't make much sense to have only one named column. 
IC Just give it a vector:
IC vect_names - c(myname1, myname2, myname3)
IC colnames(my_matrix) - vect_names

yes, exactly,  ... but read on

IC HTH,
IC Ivan

IC Le 9/27/2010 10:26, Lorenzo Cattarino a écrit :
 Hi R-users
 
 I can not change the name of one column only of my matrix.
 
 my_matrix - matrix (1:12,ncol=3)

 colnames(my_matrix)[1] - 'myname'
 
 Error in dimnames(x)- dn :
 length of 'dimnames' [2] not equal to array extent

Just read ... and maybe re-read this error message. It is
exactly on target:

The colnames  are the dimnames(.)[2] and they have the wrong length.
{ I'm too often disappointed that many R users read only the
  first, or actually the zero-th word of the 'very fine' error
  messages that we provide you with (most of the time):
  Such useRs only read up to Error  .. and are put off, saying
  It does not work or  Why can't I ...  or  
}

Now back to the original question:
What you want is to give empty column names to all but the
first column.

  mat - matrix(1:12, 3)
  colnames(mat) - c(myname, , )

And a note: giving empty column- and row-names is sometimes
desirable if just for printing.
For that reason, our  'sfsmisc' CRAN package has contained the function
empty.dimnames() for many years now (actually longer than R
exists; we had it as an S+ function in our goodies
collection). Excerpt from its help page:

 empty.dimnames package:sfsmisc R Documentation

 Empty Dimnames of an Array.

 Description:
  `Remove' all dimension names from an array for compact printing.

 Usage:
  empty.dimnames(a)
 
 Arguments:
a: an ‘array’, especially a matrix.

 Value:
  Returns ‘a’ with its dimnames replaced by empty character strings.

 Author(s):
  Bill Venables / Martin Maechler, Sept 1993.


and here the examples we provide:

 example(empty.dimnames)

empty. empty.dimnames(diag(5)) # looks much nicer
  
 1 0 0 0 0
 0 1 0 0 0
 0 0 1 0 0
 0 0 0 1 0
 0 0 0 0 1

empty. (a - matrix(-9:10, 4,5))
 [,1] [,2] [,3] [,4] [,5]
[1,]   -9   -5   -137
[2,]   -8   -4048
[3,]   -7   -3159
[4,]   -6   -226   10

empty. empty.dimnames(a) # nicer, right?
  
 -9 -5 -1 3  7
 -8 -4  0 4  8
 -7 -3  1 5  9
 -6 -2  2 6 10


__
R-help@r-project.org 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] name ONLY one column

2010-09-27 Thread Lorenzo Cattarino
Hi R-users

 

I can not change the name of one column only of my matrix.

 

my_matrix - matrix (1:12,ncol=3)

 

colnames(my_matrix)[1] - 'myname'

 

Error in dimnames(x) - dn : 

  length of 'dimnames' [2] not equal to array extent

 

thank you for your help

 

Lorenzo


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.


Re: [R] name ONLY one column

2010-09-27 Thread Ista Zahn
Hi Lorenzo,
The problem is that my_matrix does not have dimnames. See below.

my_matrix - matrix (1:12,ncol=3)
str(my_matrix) ## does not have dimnames
dimnames(my_matrix) ## dimnames is NULL

colnames(my_matrix) - myname # fails because you are trying to
alter the value of something that does not exist
## solution: Set colnames
colnames(my_matrix) - 1:dim(my_matrix)[2]
str(my_matrix) # my_matrix now has colnames
colnames(my_matrix)[1] - myname # and now we can alter them

On Mon, Sep 27, 2010 at 8:26 AM, Lorenzo Cattarino
l.cattar...@uq.edu.au wrote:
 Hi R-users



 I can not change the name of one column only of my matrix.



 my_matrix - matrix (1:12,ncol=3)



 colnames(my_matrix)[1] - 'myname'



 Error in dimnames(x) - dn :

  length of 'dimnames' [2] not equal to array extent



 thank you for your help



 Lorenzo


        [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.




-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

__
R-help@r-project.org 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.


Re: [R] name ONLY one column

2010-09-27 Thread Pedersen Jon
Hi,
It is because the column names do not exist. If you cast the matrix as a
data frame your code would work.
jon

-Original Message-
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org]
On Behalf Of Lorenzo Cattarino
Sent: 27. september 2010 10:27
To: r-help@r-project.org
Subject: [R] name ONLY one column

Hi R-users

 

I can not change the name of one column only of my matrix.

 

my_matrix - matrix (1:12,ncol=3)

 

colnames(my_matrix)[1] - 'myname'

 

Error in dimnames(x) - dn : 

  length of 'dimnames' [2] not equal to array extent

 

thank you for your help

 

Lorenzo


[[alternative HTML version deleted]]

__
R-help@r-project.org 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@r-project.org 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.


Re: [R] name ONLY one column

2010-09-27 Thread Ivan Calandra
  Hi,

I'm not sure it's even possible (and if it is I don't know how, but I'm 
no expert).

But I think it doesn't make much sense to have only one named column. 
Just give it a vector:
vect_names - c(myname1, myname2, myname3)
colnames(my_matrix) - vect_names

HTH,
Ivan


Le 9/27/2010 10:26, Lorenzo Cattarino a écrit :
 Hi R-users



 I can not change the name of one column only of my matrix.



 my_matrix- matrix (1:12,ncol=3)



 colnames(my_matrix)[1]- 'myname'



 Error in dimnames(x)- dn :

length of 'dimnames' [2] not equal to array extent



 thank you for your help



 Lorenzo


   [[alternative HTML version deleted]]

 __
 R-help@r-project.org 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.


-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Museum
Abt. Säugetiere
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calan...@uni-hamburg.de

**
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php


[[alternative HTML version deleted]]

__
R-help@r-project.org 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.