# Editing/Adding factor levels in R

**We think therefore we R**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 language="(50:120)"][/c] <- 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 language="(50:120)"][/c] <- 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 me at first, “Why would R not allow me to change/add factor [email protected]#[email protected]#?” but then Utkarsh and I had a conversation about this which made me think otherwise.

Excerpts from the conversation:

Utkarsh: It is usually not good to create data on the fly. Besides, when you create a factor variable, you should give the finite set of values it can take. This prevents future mistakes. It is called type checking. Python does not do it. R does it to some extent. C does it to some extent. Haskell does it very very strictly and it prevents about 50% of bugs from appearing. Let's say you misspell one of the levels.

In retrospect, it actually makes sense for us not to be able to add/edit the levels in factor variables. For a simple reason, we “might” make mistake, and misspelling a factor level could cause serious trouble. Lesson learnt!

**leave a comment**for the author, please follow the link and comment on their blog:

**We think therefore we R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.