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
On 2/13/2008 5:17 PM, 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?Don't use cbind() inside of data.frame(). Using cbind() coerces the variables into a matrix where all variables share a common type. I think you want this: d <- data.frame(x=I(x), y=y, z=z)> 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Joe
d <- data.frame(x=x, y=y, z=z)
sapply(d, class)
x y z
"factor" "numeric" "numeric"
d <- data.frame(x=x, y=y, z=z, stringsAsFactors=F)
sapply(d, class)
x y z
"character" "numeric" "numeric"
HTH ....
Peter Alspach
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Joe Trubisz
> Sent: Thursday, 14 February 2008 11:18 a.m.
> To: r-help at r-project.org
> Subject: [R] Creating a data.frame
>
> 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.
>
The contents of this e-mail are privileged and/or confidential to the named
recipient and are not to be used by any other person and/or organisation.
If you have received this e-mail in error, please notify the sender and delete
all material pertaining to this e-mail.
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/
On 13-Feb-08 22:17:32, 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 > JoeYou don't need to force it! It's not obvious what made you think of using cbind() internally, but it doesn't do what you intended. Simply: d<-data.frame(x=I(x),y=y,z=z) d # x y z # 1 aaa 0 0 # 2 bbb 0 0 # 3 ccc 0 0 str(d) # 'data.frame': 3 obs. of 3 variables: # $ x:Class 'AsIs' chr [1:3] "aaa" "bbb" "ccc" # $ y: num 0 0 0 # $ z: num 0 0 0 The trouble was that cbind(x=I(x),y=y,z=z) makes a matrix, and you cannot mix types in a matrix, so this will coerce all the variables to character type. So it's your original way which does the forcing! Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 13-Feb-08 Time: 23:24:30 ------------------------------ XFMail ------------------------------
Just make d <- data.frame(x,y,z). cbind forces x,y and z to be of the same type. --- Joe Trubisz <jtrubisz at mac.com> 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. >
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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.