1

I am having a heck of a time with factors injecting themselves in code where they are not preferred.

How do you remove all factors from a matrix? a vector? a data.frame?


Question update below


I thought the question would be general enough, but it is clearly not.

Factors creep in when using melt so I am looking for a way to remove the factors after I have executed the melt command. As you see from the example code below, the factor approach (not sure what to call that) enters for column 3. I presume it is because this column is text. I need to remove this factor because I am retrieving data from a matrix so a factor of 3 is meaningless (in this scenario).

names(airquality) <- tolower(names(airquality))
data <- melt(airquality, id=c("month", "day"))
is.factor(data[,3])
Aaron
  • 379
  • 5
  • 14
  • Matrices cannot contain factors. You can set `options(stringsAsFactors=FALSE)` to prevent automatic coercion from happening, but that may cause problems in others' code, as they may rely on it. – Joshua Ulrich Aug 01 '13 at 21:38
  • UGH! Clearly I need to learn to embrace factors somehow! I was hoping to avoid through the end of this project, but maybe I can't! Doing some searching now, but if y'all have any good resources I'll take what I can get here! – Aaron Aug 01 '13 at 21:58
  • 1
    This is a duplicate; Marek's [Answer](http://stackoverflow.com/a/2853231/967840) will work for you: `i <- sapply(data, is.factor); data[i] <- lapply(data[i], as.character)` – GSee Aug 01 '13 at 22:15
  • I just checked, Marek's answer does work. – Aaron Aug 01 '13 at 22:19
  • I'm not sure why you don't want factors. I know they have warts, but the great majority of the time they're invisible. They (usually) get invisibly converted to character for functions that expect character input, and they coexist happily with other kinds of data in data frames. Most times when a factor messes up your data, it's because you're making an inadvertent coercion to a matrix and things would be messed up anyway even if your factor was just a character variable. – Hong Ooi Aug 02 '13 at 01:18
  • @ Hong Ooi: The problem I face is that I am performing quite a bit of linear algebra that works efficiently with matrices. However, some data must be stored in 'data.frame' form so I have a factor which references and index of a matrix instead of the designated row/column name. – Aaron Aug 02 '13 at 01:54
  • Then you probably need to look more closely at how you're manipulating your data. The factor is not the problem. – Hong Ooi Aug 02 '13 at 01:56
  • how can you be so confident to say that? – Aaron Aug 02 '13 at 02:06

1 Answers1

1

If you want to convert a specific data frame to be factor free, I would refer to here: Convert data.frame columns from factors to characters

dataframe is named bob:

 bob <- data.frame(lapply(bob, as.character), stringsAsFactors=FALSE)

Also, if you want to read in a specific data frame and have no factors in it from the start, you can write:

file <- read.table(pathtoFile, stringsAsFactors=F)  
Community
  • 1
  • 1
cianius
  • 2,272
  • 6
  • 28
  • 41
  • 1
    this is where the "make it reproducible" folks are going to be frustrated, because my problem only occurs after using 'melt'. I'm updating my question now. – Aaron Aug 01 '13 at 21:59
  • 2
    @AaronBrown maybe [another answer](http://stackoverflow.com/a/2853231/967840) to the Question this answer links to would do what you want. – GSee Aug 01 '13 at 22:08