[R] How to create a numeric data.frame

Petr PIKAL petr.pikal at precheza.cz
Mon Jun 13 17:49:08 CEST 2011


Hi
r-help-bounces at r-project.org napsal dne 13.06.2011 17:19:39:

> Patrizio Frederic <frederic.patrizio at gmail.com> 
> 
> On Mon, Jun 13, 2011 at 4:45 PM, Barry Rowlingson
> <b.rowlingson at lancaster.ac.uk> wrote:
> > On Mon, Jun 13, 2011 at 11:06 AM, Aparna <aparna.sampath26 at gmail.com> 
wrote:
> >> Hi All
> >>
> >> I am new to R and  I am not sure of how this should be done. I have a 
matrix of
> >> 985x100 values and the class is data.frame.
> >
> >  You don't have a 'matrix' in the R sense of the word. You seem to
> > have a table of numbers which are stored in an object of class
> > 'data.frame'.
> >
> 
> but you could have one:
> 
> a <- data.frame(matrix(rnorm(100),10) # get some data
> class(a) # check for its class
> as.numeric(a) # whoops, won't work
> 
> class(as.matrix(a)) # change class, and
> as.numeric(as.matrix(a)) # bingo, it works

Which results in vector of numbers

str(as.numeric(as.matrix(a)))
 num [1:100] 0.82 -1.339 1.397 0.673 -0.461 ...

data frame is convenient list structure which can contain vectors of 
various nature (numeric, character, factor, logical, ...)
and looks quite similar to Excel table.

matrix is a vector with (2) dimensions but as it is a vector it can not 
consist from objects of different nature (class). Therefore you can have 
numeric or character matrix but not numeric and character columns in your 
matrix.

and vector is vector (numeric, character, logical,  ...) but again you can 
not mix items of different class in one vector.

Regards
Petr

> 
> PF
> 
> 
> -- 
> +-----------------------------------------------------------------------
> | Patrizio Frederic,
> | http://www.economia.unimore.it/frederic_patrizio/
> +-----------------------------------------------------------------------
> 
> ______________________________________________
> 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.



More information about the R-help mailing list