Re: [R] cbind in data.frame

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Mon, 25 Aug 2008 08:18:04 +0100 (BST)

I think the issue is that cbind applied to a vector or matrix drops classes.

It seems that data.frame() should have been used to combine columns here.

On Mon, 25 Aug 2008, Charles Plessy wrote:

> Le Sun, Aug 24, 2008 at 10:56:43PM -0700, Yuan Jian a écrit :
>> hi,
>> when I used cbind to combine columns, some contents of columns has been replaced by
>> number. in the script below, column should be aaa,bbb,ccc but I was given 1,2,3.
>> but when I change the column to vector, it gave me correct contents. can you please
>> tell me why?
>> ?
>>> d<-read.table("aaa.txt")
>
> Hello,
>
> read.table converts characters to factors. Factors will be converted
> to numbers by cbind, as in the following example:
>
>> toto <- c("aaa","bbb","aaa")
>> toto
> [1] "aaa" "bbb" "aaa"
>> as.factor(toto)
> [1] aaa bbb aaa
> Levels: aaa bbb
>> cbind(as.factor(toto))
> [,1]
> [1,] 1
> [2,] 2
> [3,] 1
>
> The 'as.is' or 'colClasses' option of read.table will help you to
> prevent the conversion of characters to factors.
>
> Have a nice day,
>
> --
> Charles Plessy
> http://charles.plessy.org
> Tsurumi, Kanagawa, Japan
>
> ______________________________________________
> 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.
>

-- 
Brian D. Ripley,                  ripley_at_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


______________________________________________ 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 25 Aug 2008 - 07:21: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 25 Aug 2008 - 08:33:59 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