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 5971497
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:31:28+00:00 2026-05-22T20:31:28+00:00

Recoding is a common practice for survey data, but the most obvious routes take

  • 0

Recoding is a common practice for survey data, but the most obvious routes take more time than they should.

The fastest code that accomplishes the same task with the provided sample data by system.time() on my machine wins.

## Sample data
dat <- cbind(rep(1:5,50000),rep(5:1,50000),rep(c(1,2,4,5,3),50000))
dat <- cbind(dat,dat,dat,dat,dat,dat,dat,dat,dat,dat,dat,dat)
dat <- as.data.frame(dat)
re.codes <- c("This","That","And","The","Other")

Code to optimize.

for(x in 1:ncol(dat)) { 
    dat[,x] <- factor(dat[,x], labels=re.codes)
    }

Current system.time():

   user  system elapsed 
   4.40    0.10    4.49 

Hint: dat <- lapply(1:ncol(dat), function(x) dat[,x] <- factor(dat[,x],labels=rc))) is not any faster.

  • 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-22T20:31:28+00:00Added an answer on May 22, 2026 at 8:31 pm

    Combining @DWin’s answer, and my answer from Most efficient list to data.frame method?:

    system.time({
      dat3 <- list()
      # define attributes once outside of loop
      attrib <- list(class="factor", levels=re.codes)
      for (i in names(dat)) {              # loop over each column in 'dat'
        dat3[[i]] <- as.integer(dat[[i]])  # convert column to integer
        attributes(dat3[[i]]) <- attrib    # assign factor attributes
      }
      # convert 'dat3' into a data.frame. We can do it like this because:
      # 1) we know 'dat' and 'dat3' have the same number of rows and columns
      # 2) we want 'dat3' to have the same colnames as 'dat'
      # 3) we don't care if 'dat3' has different rownames than 'dat'
      attributes(dat3) <- list(row.names=c(NA_integer_,nrow(dat)),
        class="data.frame", names=names(dat))
    })
    identical(dat2, dat3)  # 'dat2' is from @Dwin's answer
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like the most common relationship but for some reason I cannot get
For some time I have been trying to solve fairly common problem consisting of
I am recoding my site using CodeIgniter. This is my first time using an
I'm recoding an old site that contains a chart similar to this: alt text
I have a table recording the amount of data transferred by a given service
I'm currently recoding a bar chart in my app to make use of the
I believe eggdrop is the most active/popular bot and it's written in tcl (
I am recoding a video from my app's screen using the AVAssetWriter class. Video
I am doing audio recording using MediaRecorder, but unfortunately when I playback the recorded
I am recording acceleration data for the x-axis, so I have two values coming

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.