[R] RE: Matrix to data.frame without factors

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Apr 12 15:27:17 CEST 2002


On Fri, 12 Apr 2002, [iso-8859-1] Mäkinen Jussi wrote:

> Sorry, my previous answer slipped away because accidental key shortcut
> typing when I was trying to copy/paste the example.
> ***
>
> Thank you for your reply. Here is a sample of my data.frame (KUNTADATA):
>
> KUNTADATA[1:10,c(6:8, 20)]
>                             Kunta Period Name Asunnot 1100 intarr_14
> 1  ESPOON KAUPUNKI                     1993/1        14164  41336.27
> 2  ESPOON KAUPUNKI                     1993/2        14164        NA
> 3  ESPOON KAUPUNKI                     1994/1        14164      0.00
> 4  ESPOON KAUPUNKI                     1994/2        14164    330.29
> 5  ESPOON KAUPUNKI                     1995/1        14164      0.00
> 6  ESPOON KAUPUNKI                     1995/2        14164      0.00
> 7  ESPOON KAUPUNKI                     1996/1        14164  67277.18
> 8  ESPOON KAUPUNKI                     1996/2        14164   7860.26
> 9  ESPOON KAUPUNKI                     1997/1        14164        NA
> 10 ESPOON KAUPUNKI                     1997/2        14164 231701.05
>
>
> So there is both type of vectors: characterical and numerical. But truly - I

There are 6 types of vectors in R:  logical, integer, numeric, complex,
character and list aka `generic')!

> could just use na.to.0 when neccessary with numerical rows. But because I
> have faced this "problem" with conversions quite often I though that it
> might be a common interest to ask my question.
>
> I still cannot understand the result:
>
> > is.numeric(as.matrix(KUNTADATA[,15]))
> [1] TRUE
> > is.numeric(as.data.frame(as.matrix(KUNTADATA[,15])))
> [1] FALSE
> > mode(as.data.frame(as.matrix(KUNTADATA[1,15])))
> [1] "list"
>
> I'm sure that I just do not get the basic feature behind data.frame()
> function and that would be valuable for me and might be to somebody else as
> well.

data.frames allow colunns of different types. Matrices do not. So if
column 15 is numeric (and how can we tell, it is not in your sample!)
then as.matrix(KUNTADATA[,15]) is a one-dimensional numeric matrix.

as.data.frame(as.matrix(KUNTADATA[,15])) is a data frame with one
column.  Data frames are a class, based on a list, hence the rest of
your results.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list