[R] Odp: Creating a data.frame

From: Petr PIKAL <petr.pikal_at_precheza.cz>
Date: Mon, 18 Feb 2008 08:54:19 +0100

Hi

r-help-bounces_at_r-project.org napsal dne 13.02.2008 23:17:32:

> 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)

cbind creates a matrix which has to have all values the same type, in your case character.

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

will do what you want. Try to find out differences among various types of objects, its pros and cons from some intro documents.

Regards

Petr

>
> 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.



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. Received on Mon 18 Feb 2008 - 07:59:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Feb 2008 - 09:30:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive