Sri krishna Devarayalu Balanagu
2012-Aug-17 13:01 UTC
[R] Error: level sets of factors are different?
Why the error is coming? even though the length of outcome.new$compkey and outcome.new$armkey were exactly same. Can anyone help? setwd("D:/AZ") library("RODBC") cdb_cnct <- odbcConnectExcel("AZIF_DC_GVK_NSCLC_MSALL_287papers_02072012_141450_v1_4.xls") outcomes <- sqlFetch(cdb_cnct, "Outcomes_info") odbcClose(cdb_cnct) rm(cdb_cnct) sink("Dependency checks.log") outcomes[(outcomes$Comparator != 9999), ] -> outcome compkey <- paste(outcome$Comparator, outcome$Comparator_Subarm, sep='_') armkey <- paste(outcome$Arm_ID, outcome$SubArm_ID, sep='_') outcome.new <- cbind(outcome, compkey, armkey) invalid.ids <- outcome.new[outcome.new$compkey == outcome.new$armkey, c("Assessment_record_ID")] Error in Ops.factor(outcome.new$compkey, outcome.new$armkey) : level sets of factors are different ________________________________ Notice: The information contained in this electronic mail message is intended only for the use of the designated recipient. This message is privileged and confidential. and the property of GVK BIO or its affiliates and subsidiaries. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by telephone +91-40-66929999<tel:%2B91-40-66929999> and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). [[alternative HTML version deleted]]
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 -- Noia Raindrops noia.raindrops at gmail.com
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:>Why the error is coming? even though the length of outcome.new$compkey >and outcome.new$armkey were exactly same. >Can anyone help? > >setwd("D:/AZ") >library("RODBC") >cdb_cnct <- >odbcConnectExcel("AZIF_DC_GVK_NSCLC_MSALL_287papers_02072012_141450_v1_4.xls") >outcomes <- sqlFetch(cdb_cnct, "Outcomes_info") >odbcClose(cdb_cnct) >rm(cdb_cnct) >sink("Dependency checks.log") >outcomes[(outcomes$Comparator != 9999), ] -> outcome >compkey <- paste(outcome$Comparator, outcome$Comparator_Subarm, >sep='_') >armkey <- paste(outcome$Arm_ID, outcome$SubArm_ID, sep='_') >outcome.new <- cbind(outcome, compkey, armkey) >invalid.ids <- outcome.new[outcome.new$compkey == outcome.new$armkey, >c("Assessment_record_ID")] > >Error in Ops.factor(outcome.new$compkey, outcome.new$armkey) : > level sets of factors are different > > >________________________________ >Notice: The information contained in this electronic mail message is >intended only for the use of the designated recipient. This message is >privileged and confidential. and the property of GVK BIO or its >affiliates and subsidiaries. If the reader of this message is not the >intended recipient or an agent responsible for delivering it to the >intended recipient, you are hereby notified that you have received this >message in error and that any review, dissemination, distribution, or >copying of this message is strictly prohibited. If you have received >this communication in error, please notify us immediately by telephone >+91-40-66929999<tel:%2B91-40-66929999> and destroy any and all copies >of this message in your possession (whether hard copies or >electronically stored copies). > > [[alternative HTML version deleted]] > >______________________________________________ >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.
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:> Why the error is coming? even though the length of outcome.new$compkey and outcome.new$armkey were exactly same. > Can anyone help? > > setwd("D:/AZ") > library("RODBC") > cdb_cnct <- odbcConnectExcel("AZIF_DC_GVK_NSCLC_MSALL_287papers_02072012_141450_v1_4.xls") > outcomes <- sqlFetch(cdb_cnct, "Outcomes_info") > odbcClose(cdb_cnct) > rm(cdb_cnct) > sink("Dependency checks.log") > outcomes[(outcomes$Comparator != 9999), ] -> outcome > compkey <- paste(outcome$Comparator, outcome$Comparator_Subarm, sep='_') > armkey <- paste(outcome$Arm_ID, outcome$SubArm_ID, sep='_') > outcome.new <- cbind(outcome, compkey, armkey) > invalid.ids <- outcome.new[outcome.new$compkey == outcome.new$armkey, c("Assessment_record_ID")] > > Error in Ops.factor(outcome.new$compkey, outcome.new$armkey) : > level sets of factors are different > > > ________________________________ > Notice: The information contained in this electronic mail message is intended only for the use of the designated recipient. This message is privileged and confidential. and the property of GVK BIO or its affiliates and subsidiaries. If the reader of this message is not the intended recipient or an agent responsible for delivering it to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution, or copying of this message is strictly prohibited. If you have received this communication in error, please notify us immediately by telephone +91-40-66929999<tel:%2B91-40-66929999> and destroy any and all copies of this message in your possession (whether hard copies or electronically stored copies). > > [[alternative HTML version deleted]] > > ______________________________________________ > 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.