Editing/Adding factor levels in R
I was trying to change few levels in my factor variable by simply coercing characters on that factor variable but it dint seem to work.
data(iris)<br>iris$Species[c(50:120)] <- rep("Random", 71)<br>
## Warning: invalid factor level, NAs generated<br>
iris$Species<br>
## [1] setosa setosa setosa setosa setosa setosa setosa <br>## [8] setosa setosa setosa setosa setosa setosa setosa <br>## [15] setosa setosa setosa setosa setosa setosa setosa <br>## [22] setosa setosa setosa setosa setosa setosa setosa <br>## [29] setosa setosa setosa setosa setosa setosa setosa <br>## [36] setosa setosa setosa setosa setosa setosa setosa <br>## [43] setosa setosa setosa setosa setosa setosa setosa <br>## [50] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br><br>## [57] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br>## [64] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br><br>## [71] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br>## [78] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br><br>## [85] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br>## [92] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br><br>## [99] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br>## [106] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br><br>## [113] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <br>## [120] <NA> virginica virginica virginica virginica virginica virginica<br>## [127] virginica virginica virginica virginica virginica virginica virginica<br>## [134] virginica virginica virginica virginica virginica virginica virginica<br>## [141] virginica virginica virginica virginica virginica virginica virginica<br>## [148] virginica virginica virginica<br>## Levels: setosa versicolor virginica<br><br>Well, I did find a way to find a work around for that by doing this:
iris$Species <- as.character(iris$Species)<br>iris$Species[c(50:120)] <- rep("Random", 71)<br>iris$Species <- as.factor(iris$Species)<br>iris$Species<br>
## [1] setosa setosa setosa setosa setosa setosa setosa <br>## [8] setosa setosa setosa setosa setosa setosa setosa <br>## [15] setosa setosa setosa setosa setosa setosa setosa <br>## [22] setosa setosa setosa setosa setosa setosa setosa <br>## [29] setosa setosa setosa setosa setosa setosa setosa <br>## [36] setosa setosa setosa setosa setosa setosa setosa <br>## [43] setosa setosa setosa setosa setosa setosa setosa <br>## [50] Random Random Random Random Random Random Random <br>## [57] Random Random Random Random Random Random Random <br>## [64] Random Random Random Random Random Random Random <br>## [71] Random Random Random Random Random Random Random <br>## [78] Random Random Random Random Random Random Random <br>## [85] Random Random Random Random Random Random Random <br>## [92] Random Random Random Random Random Random Random <br>## [99] Random Random Random Random Random Random Random <br>## [106] Random Random Random Random Random Random Random <br>## [113] Random Random Random Random Random Random Random <br>## [120] Random virginica virginica virginica virginica virginica virginica<br>## [127] virginica virginica virginica virginica virginica virginica virginica<br>## [134] virginica virginica virginica virginica virginica virginica virginica<br>## [141] virginica virginica virginica virginica virginica virginica virginica<br>## [148] virginica virginica virginica<br>## Levels: Random setosa virginica<br>This problem annoyed ... [Read more...]