[R] cbind() and factors.

Stephane DRAY dray at biomserv.univ-lyon1.fr
Fri Dec 10 15:39:36 CET 2004


cat is a data.frame,
so cbind is use for a data.frame

and
?data.frame tell us that:

  Character variables passed to 'data.frame' are converted
      to factor columns unless protected by 'I'.

PS : it is not good ides to call your data.frame cat as there is a cat 
function.



At 09:19 10/12/2004, michael watson (IAH-C) wrote:
>Hi
>
>I'm seeing some "odd" behaviour with cbind().  My code is:
>
> > cat <- read.table("cogs_category.txt", sep="\t", header=TRUE,
>quote=NULL, colClasses="character")
> > colnames(cat)
>[1] "Code"        "Description"
> > is.factor(cat$Code)
>[1] FALSE
> > is.factor(cat$Description)
>[1] FALSE
> > is.factor(rainbow(nrow(cat)))
>[1] FALSE
> > cat <- cbind(cat,"Color"=rainbow(nrow(cat)))
> > is.factor(cat$Color)
>[1] TRUE
> > ?cbind
>
>I read a text file in which has two columns, Code and Description.
>Neither of these are factors.  I want to add a column of colours to the
>data frame using rainbow().  The rainbow function also does not return a
>factor.  However, if I cbind my data frame (which has no factors in it)
>and the results of rainbow() (which is a vector, not a factor), then for
>some reason the new column is a factor...??
>
>Mick
>
>
>Michael Watson
>Head of Informatics
>Institute for Animal Health,
>Compton Laboratory,
>Compton,
>Newbury,
>Berkshire RG20 7NN
>UK
>
>Phone : +44 (0)1635 578411 ext. 2535
>Mobile: +44 (0)7990 827831
>E-mail: michael.watson at bbsrc.ac.uk
>
>______________________________________________
>R-help at 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

Stéphane DRAY
-------------------------------------------------------------------------------------------------- 

Département des Sciences Biologiques
Université de Montréal, C.P. 6128, succursale centre-ville
Montréal, Québec H3C 3J7, Canada

Tel : (514) 343-6111 poste 1233         Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca
-------------------------------------------------------------------------------------------------- 

Web                                          http://www.steph280.freesurf.fr/




More information about the R-help mailing list