[R] cbind() and factors.

michael watson (IAH-C) michael.watson at bbsrc.ac.uk
Fri Dec 10 15:19:16 CET 2004


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




More information about the R-help mailing list