RES: [R] data.frame to matrix

Gustavo Pinheiro gustavo at estatcamp.com.br
Mon Feb 9 23:10:00 CET 2004


Hi Duncan,

You were right. It was a vector and not a data.frame that I was dealing
with. But still I am having dificulties. Please, take a look at some output
(I am using R Commander GUI by the way):

R-cmdr> print(b) 
 [1] 0.70 0.85 0.80 0.70 0.75 0.75 0.80 0.70 0.80 0.75 0.80 0.79 0.78 0.75
0.76
[16] 0.70 0.70 0.70 0.80 0.80 0.70 0.65 0.60 0.70 0.55 0.80 0.65 0.60 0.70
 [1] 0.65 0.75 0.80 0.70 0.65 0.75 0.65 0.80 0.85 0.70 0.80 0.79 0.78 0.85
0.76
[16] 0.75 0.85 0.60 0.80 0.75 0.85 0.85 0.65 0.70 0.65 0.65 0.75 0.60 0.60
 [1] 0.65 0.75 0.80 0.70 0.65 0.75 0.65 0.80 0.85 0.70 0.80 0.79 0.78 0.85
0.76
[16] 0.75 0.85 0.60 0.80 0.75 0.85 0.85 0.65 0.70 0.65 0.65 0.75 0.60 0.60

R-cmdr> print(length(b)) 
[1] 29
[1] 29
[1] 29

R-cmdr> print(b[1])
[1] 0.70
[1] 0.65
[1] 0.65

R-cmdr> print(b[1,])
Error: incorrect number of dimention 

R-cmdr> print(b[,2])
Error: incorrect number of dimention

Looking at this output I figured b is a vector of vectors (3 to be exact).
The thing is I want to create a matrix made of b's lines (which were
originaly columns in my dataset) as columns, but I could not find a way to
retrieve a line from b in a way similar to what one would do with dataframes
(dataframe[1,]). You can see in the above output that a call to b[1,]
returns an error and a call to b[1] returns the first column. So, how to
retrieve a whole line? Keep in mind that the size of b is not known before
hand, so if it turns out to be necessary to go through each element another
problem would arise: a call to length() returns the length of each one of
these 3 vectors, but not the length of the containg vector b which is 3.

Was I clear enough? My english is not so great ;-)

Regards,

Gustavo.

-----Mensagem original-----
De: Duncan Murdoch [mailto:dmurdoch at pair.com] 
Enviada em: Monday, February 09, 2004 5:25 PM
Para: Gustavo Pinheiro
Cc: r-help at stat.math.ethz.ch
Assunto: Re: [R] data.frame to matrix


On Mon, 9 Feb 2004 17:47:36 -0300, "Gustavo Pinheiro"
<gustavo at estatcamp.com.br> wrote :

>Hello all,
>
>I've had trouble converting a data.frame to a matrix (numeric) using 
>either
>data.matrix() and as.matrix().
>After executing one of those I end up with another data.frame with only the
>first column of the original data.frame.
>I use a window (tcltk) to let the user choose the columns he wants and then
>I retrieve them using the following:
>
>varstemp <- .numeric[as.numeric(tkcurselection(subgroupsBox)) + 1]
>
>where ".numeric" is the original (complete) data.frame.
>
>Any ideas why is this happening? I'm using R1.8.1 in WinXP by the way.

I'd guess "as.numeric(tkcurselection(subgroupsBox)) + 1" isn't returning
what you think it's returning, or maybe .numeric isn't in the form you
think.

I'd also recommend using both row and column indices when working with data
frames.  What you have selects columns from a data.frame, but not from a
matrix (assuming that the index is a vector of integers).  I find it's safer
to treat data.frames as matrices whenever you can, i.e. use a blank row
index

varstemp <- .numeric[ , as.numeric(tkcurselection(subgroupsBox)) + 1] 

Duncan Murdoch




More information about the R-help mailing list