[R] Creating a data.frame
Peter Alspach
PAlspach at hortresearch.co.nz
Thu Feb 14 00:11:40 CET 2008
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.
More information about the R-help
mailing list