[R] Creating a data.frame

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Thu Feb 14 00:13:37 CET 2008


Hi Joe,

cbind coerces the data to be the same type.  Much nicer is:

d <- data.frame(x=x, y=y, z=z)

Cheers

Andrew

On Wed, Feb 13, 2008 at 05:17:32PM -0500, Joe Trubisz wrote:
> OK...newbie question here.
> Either I'm reading the docs wrong, or I'm totally confused.
> 
> Given the following:
> 
> x<-c("aaa","bbb","ccc")
> y<-rep(0,3)
> z<-rep(0,3)
> 
> is.character(x)
> [1] TRUE
> 
> is.numeric(y)
> [1] TRUE
> 
> Now...I want to create a data frame, but keep the data types.
> In reading the docs, I assume you do it this way:
> 
> d<-data.frame(cbind(x=I(x),y=y,z=z)
> 
> But, when I do str(d), I get the following:
> 
> 'data.frame':	3 obs. of  3 variables:
>   $ x: Factor w/ 3 levels "aaa","bbb","ccc": 1 2 3
>   $ y: Factor w/ 1 level "0": 1 1 1
>   $ z: Factor w/ 1 level "0": 1 1 1
> 
> I thought the I() prevents character from becoming factors, right?
> Secondly, how do I force y and z in the data frame to become numeric?
> 
> Thanks in advance
> Joe
> 
> ______________________________________________
> 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.
> 
> -- 
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-6410
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
http://www.ms.unimelb.edu.au/~andrewpr
http://blogs.mbs.edu/fishing-in-the-bay/



More information about the R-help mailing list