Hi,
At the 1st step, you only assign a name for the 1st row. However, fred has 4
rows which mean you need to assign 4 rownames for these 4 rows.

At the 2nd step, you only "CHANGE" the 1st rowname from 1 to APPLE


If I am wrong please correct me.

Thanks



-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On
Behalf Of snubian
Sent: Monday, March 02, 2009 4:59 PM
To: r-help@r-project.org
Subject: [R] Error setting rowname if rowname currently NULL


Hi,

My first post here and new to R so please bear with me (long time programmer
though, helping a friend with some scripts).

I've noticed a behaviour when using rownames() that I think is odd,
wondering if I'm doing something wrong.

To illustrate, say I create a very simple matrix (called fred):

fred<-matrix(,4,2)

It looks like this:

     [,1] [,2]
[1,]   NA   NA
[2,]   NA   NA
[3,]   NA   NA
[4,]   NA   NA

If I now try and set a row name for one of the rows (say the first row) to
"APPLE", by doing this:

rownames(fred)[1] <- "APPLE"

I get an error:

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

However, I found that if I first set all the rownames to anything at all, by
using say:

rownames(fred) <- c(1:4)

Which gives me:

  [,1] [,2]
1   NA   NA
2   NA   NA
3   NA   NA
4   NA   NA

Then my desired command works, and thus:

rownames(fred)[1] <- "APPLE"

Gives me what I want:

      [,1] [,2]
APPLE   NA   NA
2       NA   NA
3       NA   NA
4       NA   NA

So, what this says to me is that to set the row names INDIVIDUALLY, they
first need to be set to something (anything!).

For what I am doing, I need to set the row names one at a time, as I iterate
through a loop. So I found that to do this I first had to set the rownames
to some dummy values as above. Then it works fine. But this seems a little
kludgy and unnecessary to me, and I am wondering what I am doing wrong. Have
just started in R so fumbling my way through somewhat.

Any suggestions would be appreciated, thanks!


-- 
View this message in context:
http://www.nabble.com/Error-setting-rowname-if-rowname-currently-NULL-tp2229
8797p22298797.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help@r-project.org mailing list

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.

Reply via email to