Skip to content

Factor Madness

7 messages · Tony Plate, John Kane, Johannes Graumann

#
From ?cbind:

Data frame methods
The cbind data frame method is just a wrapper for data.frame(..., 
check.names = FALSE). This means that it will split matrix columns in data 
frame arguments, and convert character columns to factors unless 
stringsAsFactors = TRUE is passed.

(I'm guessing 'spectrum' is a data.frame before the code fragment you've shown)

hope this helps,

Tony Plate
Johannes Graumann wrote:
#
Whoops, it looks like there's a typo in ?cbind (R version 2.6.0 Patched 
(2007-10-11 r43143)), and I blindly copied it into my message.

That should read (emphasis added):

"and convert character columns to factors unless stringsAsFactors = 
***FALSE***"

Here's an example:

 > x <- data.frame(X=1:3)
 > sapply(cbind(x, letters[1:3]), class)
            X letters[1:3]
    "integer"     "factor"
 > sapply(cbind(x, letters[1:3], stringsAsFactors=FALSE), class)
            X letters[1:3]
    "integer"  "character"
 >

Thanks to Mark Leeds for pointing that out to me in a private message!

(I see this still in the source at 
https://svn.r-project.org/R/trunk/src/library/base/man/cbind.Rd -- is that 
the right place to look for the latest source to make sure it hasn't been 
fixed already?)

-- Tony Plate
Tony Plate wrote:
#
Why is class(spectrum[["Ion"]]) after this "factor"?

spectrum <- cbind(spectrum,Ion=rep("",
nrow(spectrum)),Deviation.AMU=rep(0.0, nrow(spectrum)))

slowly going crazy ...

Joh
#
Yep, but I figured that out quite fast ;0)
Thanks for giving me a hand ... you want believe a many times I skimmed the
cbind help without actually seeing this ... well, it was 0:30 ...

Thanks again, Joh
Tony Plate wrote:

            
#
What was spectrum orginally?


--- Johannes Graumann <johannes_graumann at web.de>
wrote:
#
As Tony assumed: a data frame.

Joh
John Kane wrote:

            
#
You may be haning Ion coerced into a factor.  Have a
look at
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/98260.html
for some discussion of this. 

I find that I usually set 
options(stringsAsFactors = FALSE)  just because of
this but as Gabor points out it may have its own
disadvantages in sharing code.  I spent 2 hours once
trying to get an example to work only to find that the
code assume options(stringsAsFactors = TRUE).

--- Johannes Graumann <johannes_graumann at web.de>
wrote: