[R] Removing and restoring factor levels

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Thu 13 Oct 2005 - 23:55:18 EST

I'm doing a big slow computation, and profiling shows that it is spending a lot of time in match(), apparently because I have code like

x %in% listofxvals

Both x and listofxvals are factors with the same levels, so I could probably speed this up by stripping off the levels and just treating them as integer vectors, then restoring the levels at the end.

What is the safest way to do this? I am worried that at some point x and listofxvals will *not* have the same levels, and the optimization will give the wrong answer. So I need code that guarantees they have the same coding.

I think this works, where "master" is a factor with the master list of levels (guaranteed to be a superset of the levels of x and listofxvals), but can anyone spot anything that might go wrong?

# Strip the levels

x <- as.integer( factor(x), levels = levels(master) )

# Restore the levels

x <- structure( x, levels = levels(master), class = "factor" )

Thanks for any advice...

Duncan Murdoch

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Oct 14 00:09:56 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:50:14 EST