Skip to content

Error: level sets of factors are different?

4 messages · Sri krishna Devarayalu Balanagu, Noia Raindrops, Jeff Newmiller +1 more

#
The cbind method to data.frame is just a wrapper for data.frame(...). So character columns are converted to factors.

dat <- cbind(data.frame(x = 1:3), a = c("a", "b", "c"), b = c("a", "a", "c"))
str(dat)
## 'data.frame': 3 obs. of  3 variables:
##  $ x: int  1 2 3
##  $ a: Factor w/ 3 levels "a","b","c": 1 2 3
##  $ b: Factor w/ 2 levels "a","c": 1 1 2
dat$a == dat$b
## Error in Ops.factor(dat$a, dat$b) : level sets of factors are different

as.character(dat$a) == as.character(dat$b)
## [1]  TRUE FALSE  TRUE

dat <- cbind(data.frame(x = 1:3), a = c("a", "b", "c"), b = c("a", "a", "c"), stringsAsFactors = FALSE)
str(dat)
## 'data.frame': 3 obs. of  3 variables:
##  $ x: int  1 2 3
##  $ a: chr  "a" "b" "c"
##  $ b: chr  "a" "a" "c"
dat$a == dat$b
## [1]  TRUE FALSE  TRUE
#
The length of a vector of yes/no answers has little to do with the number of possible responses (2). Factors keep both pieces of information.

If you only have yes responses in your data set, when converted to factor you would have to tell R that other responses  were possible.

I prefer to avoid working with factors until I need them.  Use the str function to investigate where factors are being introduced and either use options to keep the data as character or convert it back to character if you can't figure that out.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
Sri krishna Devarayalu Balanagu <balanagudevarayulu at gvkbio.com> wrote:

            
#
Hello,

The message is not about lengths, it's about sets. It means that the two 
factors, compkey and armkey don't have the same levels, which you can 
see with

levels(compkey)
levels(armkey)

and test for equality.

Also, your post's prologue doesn't have a direct relation with the 
error, you could have skipped the lines up to, including, sink(). It's 
much better to provide an actual data example:

dput( head(outcome, 50) )   # paste the output of this in a post.

Hope this helps,

Rui Barradas
Em 17-08-2012 14:01, Sri krishna Devarayalu Balanagu escreveu: