1

I am trying to re-factor across all column factors in a data frame to reduce the number of factor levels. Example is in my case, after I removed about 50% of rows in my data frame, a lot of columns have values in their factor levels that no longer exist, and I want to remove them. The data frame contains quite a lot of columns which are factors, so re-factoring them one at a time was getting tedious for me.

There's another question in SO called refactor data.frame column values but it doesn't address my question, so I thought I'll put in the question here as reference.

Community
  • 1
  • 1
Ricky
  • 4,616
  • 6
  • 42
  • 72

1 Answers1

1

I found some answers here, and the one I chose for my purpose was from Prof Brian Ripley:

ind <- sapply(DF, is.factor)
DF[ind] <- lapply(DF[ind], "[", drop=TRUE) 

where the DF is the dataframe.

Ricky
  • 4,616
  • 6
  • 42
  • 72