I have an R dataframe and I’m trying to subtract one column from another. I extract the columns using the $ operator but the class of the columns is ‘factor’ and R won’t perform arithmetic operations on factors. Are there special functions to do this?
I have an R dataframe and I’m trying to subtract one column from another.
Share
If you really want the levels of the factor to be used, you’re either doing something very wrong or too clever for its own good.
If what you have is a factor containing numbers stored in the levels of the factor, then you want to coerce it to numeric first using
as.numeric(as.character(...)):You can see the difference between accessing the factor indices and assigning the factor contents here:
Timings vs. an alternative approach which only does the conversion on the levels shows it’s faster if levels are not unique to each element:
Therefore, if
length(levels(dat$f)) < length(dat$f), useas.numeric(levels(dat$f))[dat$f]for a substantial speed gain.If
length(levels(dat$f))is approximately equal tolength(dat$f), there is no speed gain: