Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:39:24+00:00 2026-05-24T09:39:24+00:00

I have an R dataframe and I’m trying to subtract one column from another.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:39:25+00:00Added an answer on May 24, 2026 at 9:39 am

    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(...)):

    dat <- data.frame(f=as.character(runif(10)))
    

    You can see the difference between accessing the factor indices and assigning the factor contents here:

    > as.numeric(dat$f)
     [1]  9  7  2  1  4  6  5  3 10  8
    > as.numeric(as.character(dat$f))
     [1] 0.6369432 0.4455214 0.1204000 0.0336245 0.2731787 0.4219241 0.2910194
     [8] 0.1868443 0.9443593 0.5784658
    

    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:

    dat <- data.frame( f = sample(as.character(runif(10)),10^4,replace=TRUE) )
    library(microbenchmark)
    microbenchmark(
      as.numeric(as.character(dat$f)),
      as.numeric( levels(dat$f) )[dat$f] ,
      as.numeric( levels(dat$f)[dat$f] ),
      times=50
      )
    
                                  expr     min      lq  median      uq     max
    1  as.numeric(as.character(dat$f)) 7835865 7869228 7919699 7998399 9576694
    2 as.numeric(levels(dat$f))[dat$f]  237814  242947  255778  270321  371263
    3 as.numeric(levels(dat$f)[dat$f]) 7817045 7905156 7964610 8121583 9297819
    

    Therefore, if length(levels(dat$f)) < length(dat$f), use as.numeric(levels(dat$f))[dat$f] for a substantial speed gain.

    If length(levels(dat$f)) is approximately equal to length(dat$f), there is no speed gain:

    dat <- data.frame( f = as.character(runif(10^4) ) )
    library(microbenchmark)
    microbenchmark(
      as.numeric(as.character(dat$f)),
      as.numeric( levels(dat$f) )[dat$f] ,
      as.numeric( levels(dat$f)[dat$f] ),
      times=50
      )
    
                                  expr     min      lq  median      uq      max
    1  as.numeric(as.character(dat$f)) 7986423 8036895 8101480 8202850 12522842
    2 as.numeric(levels(dat$f))[dat$f] 7815335 7866661 7949640 8102764 15809456
    3 as.numeric(levels(dat$f)[dat$f]) 7989845 8040316 8122012 8330312 10420161
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe in R that I loaded from a CSV file. One
I'd like to melt the dataframe so that in one column I have dates
I have a dataframe (14.5K rows by 15 columns) containing billing data from 2001
I have a dataframe. first column contains my the predictive score (range from 0
I have a dataframe with a few columns, one of those columns is ranks,
I have a dataframe with Address, City, State, Zip entities. From there, I'm trying
Let's have a dataframe with long strings in one column: df<-data.frame(short=rnorm(10,0,1),long=replicate(10,paste(rep(sample(letters),runif(1,5,8)),collapse=))) How could I
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe with a column of integers that I would like to
Suppose I have a dataframe like this one: df <- data.frame (id = c(a,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.