[R] Odp: storing text and decimal values in a matrix form

Petr PIKAL petr.pikal at precheza.cz
Tue Sep 11 18:09:59 CEST 2007


Hi

Gonçalo Ferraz <gferraz29 at gmail.com> napsal dne 11.09.2007 16:35:57:

> Thanks! What is the easiest way of declaring an empty data frame. Is 
there 
> anything similar to:
> matrix(0,x,y)
> In the help files, I am only finding longer commands that ask me to 
specify 
> each individual column.

You can either do

dat<-data.frame(x=rep(NA,10), y=rep(NA,10))

or to create empty matrix and then transfer it to data frame via 
as.data.frame. From what you wrote I assume that you fill data during some 
cycle. If yes, you shall reconsider it as there can be better options.

Regards
Petr

> G.
> 
> On Sep 11, 2007, at 9:56 AM, Petr PIKAL wrote:
> 
> Hi
> 
> Although not specifically stated in help page, matrix is a vector with 
dim 
> attribute. Therefore it can have only one type of values either numeric 
or 
> character or logical or factor. So if you want textual and numeric 
values 
> you probably have to stick with data frames.
> 
> Regards
> Petr
> petr.pikal at precheza.cz
> 
> r-help-bounces at r-project.org napsal dne 11.09.2007 15:43:20:
> 
> Hi,
> 
> I would like to store some text and decimal values in the following 
> form:
> 
> name   dec.val   dec.val   ...   dec.val
> name   dec.val   dec.val   ...   dec.val
> ...
> name   dec. val    dec.val   ...   dec.val
> 
> To do so, I created a matrix of the necessary size (x row per y cols) 
> with the command
> 
> matrixname <- matrix(0,x,y)
> 
> and proceeded to fill in the matrix with information drawn from a 
> data frame.
> 
> The problem is that the resulting matrix only has integers at the end 
> of the filling process.
> 
> When I ask:
> class(dataframename[i,j])
> The answer is "factor"
> 
> What is the best way of solving the problem. Should I use a data 
> structure other than a matrix? Or should I make the elements be a 
> different class (other than "factor") when I assign them to the matrix?
> 
> Thanks for any help!
> 
> G.
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
> 
> Gonçalo Ferraz, PhD
> Projeto Dinâmica Biológica de Fragmentos Florestais
> Instituto Nacional de Pesquisas da Amazônia &
> Smithsonian Tropical Research Institute
> CP 478, Av. André Araújo, 1753
> 69011-970 Manaus AM
> Brasil
> Tel: (92) 3643-3229



More information about the R-help mailing list