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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:18:46+00:00 2026-06-02T04:18:46+00:00

I have a data frame and I’m trying to take a factor variable and

  • 0

I have a data frame and I’m trying to take a factor variable and keep only the top 31 levels and make all the other levels some generic level.

I need to do this across several vectors so I figured I’d create function, but I’m not having much luck. I think I need to somehow use mapply or Vectorize but I don’t think I’m doing it properly as I get error messages about being unable to allocate 3.6 gigs of memory.

This is the function where x is the vector and topCount is the number of levels to keep

createFactor <-function(x, topCount){
    table1 <- data.frame(table(x))
    table1 <- table1[order(-table1$Freq),]
    noChange <- table1$Var1[1:topCount]
    newVals1 <- factor(ifelse(x %in% noChange, x, "-1000"))
    newVals1
}

I’d like to be able to write something like this:

df1$topLevels <- createFactor(df1$fact1, 31)

Any suggestions ?

  • 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-06-02T04:18:48+00:00Added an answer on June 2, 2026 at 4:18 am

    I’m not completely certain about the performance characteristics of this, but I probably would have written this function more like so:

    topK <- function(x,k){
        tbl <- tabulate(x)
        names(tbl) <- levels(x)
        x <- as.character(x)
        levelsToKeep <- names(tail(sort(tbl),k))
        x[!(x %in% levelsToKeep)] <- '-1000'
        factor(x)
    }
    

    where I’ve used tabulate rather than table because I suspect is may be faster (which seems important in your case) although I haven’t tested this to see how much faster it would actually be.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data.frame that contains several factors and i want to rename factor levels
I have data frame with some numerical variables and some categorical factor variables. The
I have a data frame of integers that is a subset of all of
I have a data frame filled with factor columns, and I want to add
I have a data frame with 3 variables, which are all wind speeds. I
I have a data frame with several columns, one of which is a factor
I have a data frame and tried to select only the observations I'm interested
I have a data frame with three columns and I'd like to make a
I have a data frame with a quantitative variable, x, and several different factors,
I have a data frame with 3 variables: place, time, and value (P, T,

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.