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

  • SEARCH
  • Home
  • 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 8472315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:01:58+00:00 2026-06-10T17:01:58+00:00

So…I have a large data set with a variable that has many categories. I

  • 0

So…I have a large data set with a variable that has many categories. I want to create new variables that group some of those categories into one.

I could do that with a conditional statement, but given the amount of categories it would take me forever to go one line at the time. Also, while my original variable is numeric, the values themselves are random so I can´t use logical or range statements.

How do I create this conditional variable based on many particular values?

I tried the following, but without success. Below is an example of the different categories I want to group into one.

classes <- c(549,162,210,222,44,96,62,208,525,202,149,442,427,
      564,423,106,422,546,205,560,127,536,34,261,568,
      366,524,401,548,95,156,8,528, 430,527,556,203,554,523,
      501,530,55,252,585,19,540,71,204,502,504, 196,436,48,
      102,526,201,521,23,558,552,118,416,117,216,510,494,
      516,544,518)

So this seemed pretty intuitive to me, but it doesn´t work.

df$chem<- cbind(ifelse(df$class == classes ,1,0))

Needless to say I´m a beginner, and this is probably not so hard to do, but I´ve been looking for a solution to this particular problem and I can´t seem to find it. What am I missing? Thanks!

  • 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-10T17:02:00+00:00Added an answer on June 10, 2026 at 5:02 pm

    You are looking for %in% not ==

    eg

    df$chem <- cbind(ifelse(df$class %in% classes ,1,0))
    

    or using the logical to numeric conversion

    df$chem <-  as.numeric(df$class %in% classes)
    

    if you want individual dummy variables for all the categories in df$class then you can use the class.ind function in the package nnet (which is shipped as a recommended package)

    library(nnet)
    
    class_ind <- class.ind(df$class)
    # add if you want to combine with the original
    df_ind <- do.call(cbind, list(df, class.ind(df$class))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element like this: <span class=tool_tip title=The full title>The ful&#8230;</span> This seems
so I did $subject = 'sakdlfjsalfdjslfad <a href=something/8230>lol is that true?</a> lalalala'; $subject =
I have this xml <entry id=1008 section=articles> <excerpt><p>&#8230; in Richtung „Aus für Tierversuche. Kosmetik-Fertigprodukte
I see that some rss on xml have strange strings. For example, ... is
I have a PDB file. Now it has two parts separated by TER. Before
After much research, I have found that Laravel is very nice. I have a
I have been making a wordpress template. i got stuck at some place... the
what about this one: I want to format the currentTime displayed by a videoPlayer
(tl;dr: see summary at the bottom.) I am implementing an application that pulls content
I want to sign a message according to the RSA-SHA1 Signature Method in OAuth

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.