Getting the error “level sets of factors are different” when running a for loop

后端 未结 2 684
你的背包
你的背包 2020-12-10 12:54

I have the following 3 tables:

AggData <- structure(list(Path = c(\"NonBrand\", \"Brand\", \"NonBrand,NonBrand\", 
\"Brand,Brand\", \"NonBrand,NonBrand,No         


        
相关标签:
2条回答
  • 2020-12-10 13:23

    FinalTable[2,1] and breakVector[1,1] do not have the same levels:

    > FinalTable[2,1]
    [1] Brand
    Levels: Brand NonBrand
    > breakVector[1,1]
    [1] NonBrand
    Levels: NonBrand
    

    This is easily fixed by using

    breakVector[,1] <- factor(breakVector[,1], levels=c("Brand", "NonBrand"))
    

    or, more generally

    breakVector[,1] <- factor(breakVector[,1], levels=levels(FinalTable[,1]))
    
    0 讨论(0)
  • 2020-12-10 13:25

    Perhaps, it will better compare both variables like a string:

    if (as.character(FinalTable [2,1]) == as.character(breakVector[1,1])) {
         FinalTable$attributed_result[2] <- FinalTable$attributed_result[2] + 
         breakVector[1,2] * AggData$conv_count[3];
         break}
    
    0 讨论(0)
提交回复
热议问题