Re: [R] as.data.frame(cbind()) transforming numeric to factor?

From: Martin Maechler <maechler_at_stat.math.ethz.ch>
Date: Sat 19 Aug 2006 - 03:10:01 EST

>>>>> "Tom" == Tom Boonen <tom.boonen.maiden@gmail.com> >>>>> on Fri, 18 Aug 2006 11:16:45 -0400 writes:

    Tom> Thanks everybody. I recognize my mistake now.
    Tom> I think    as.data.frame(cbind(x.1,x.2),stringsAsFactors = FALSE)
    Tom> would be a good idea.

I think

        data.frame(x.1, x.2 = I(x.2))

would be a considerably better idea.

[ The use of I(.) for preventing coercion to factors   is a much older and "S-like" way ]

Martin

    Tom> Tom

    Tom> On 8/18/06, Prof Brian Ripley <ripley@stats.ox.ac.uk> wrote:
>> On Fri, 18 Aug 2006, Tom Boonen wrote:
>>
>> > Dear List,
>> >
>> > why does as.data.frame(cbind()) transform numeric variables to
>> > factors, once one of the other variablesused is a character vector?
>> >
>> > #
>> > x.1 <- rnorm(10)
>> > x.2 <- c(rep("Test",10))
>> > Foo <- as.data.frame(cbind(x.1))
>> > is.factor(Foo$x.1)
>> >
>> > Foo <- as.data.frame(cbind(x.1,x.2))
>> > is.factor(Foo$x.1)
>> > #
>> >
>> > I assume there is a good reason for this, can somebody explain? Thanks.
>>
>> Only if you can explain the good reason why you did not just use
>> data.frame(x.1, x.2)!
>>
>> cbind() makes a matrix out of vectors, here a character matrix. And then
>> as.data.frame() converts character columns to factors.
>>
>> --
>> Brian D. Ripley, ripley@stats.ox.ac.uk
>> Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>> University of Oxford, Tel: +44 1865 272861 (self)
>> 1 South Parks Road, +44 1865 272866 (PA)
>> Oxford OX1 3TG, UK Fax: +44 1865 272595
>>

    Tom> ______________________________________________
    Tom> R-help@stat.math.ethz.ch mailing list
    Tom> https://stat.ethz.ch/mailman/listinfo/r-help
    Tom> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html     Tom> and provide commented, minimal, self-contained, reproducible code.

R-help@stat.math.ethz.ch 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 Sat Aug 19 03:14:56 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 19 Aug 2006 - 08:20:48 EST.

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