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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:44:31+00:00 2026-06-14T00:44:31+00:00

I have a dataset with a categorical variable hospital_code which has 10 levels. The

  • 0

I have a dataset with a categorical variable hospital_code which has 10 levels.

The program that I am running loops through and takes a subset of the data such that the variable compLbl contains exactly 2 of the 10 hospital_codes so that they can be compared to each other. I now have a situation where in each loop, I need compLbl to be binary coded (1s, and 0s).

If I just take the subset data from the first loop in which the possible values for compLbl are AMH, and BJH, I can easily do this as follows:

nData$compLbl2 = with(nData,(ifelse(compLbl == "AMH", 1,0)))

And get data that looks like this:

head(nData)
compLbl outLbl Race_Code Age Complexity_Subclass_Code compLbl2
1     AMH      0         W  63                        1        1
2     AMH      0         W  44                        2        1
3     AMH      0         W  88                        3        1
4     BHC      0         W  64                        1        0
5     BHC      0         W  61                        2        0
6     BHC      0         W  61                        1        0

How can I generalize this so that no matter what two values are in compLbl it will binary code them? My thought was to possibly do this by referencing factor level 1 for whatever two values are present in the factor variable compLbl. Like this:

nData$compLbl2 = with(nData,(ifelse(FACTORLEVEL(compLbl) == 1, 1,0)))

Where in my above example FACTORLEVEL(compLbl) would return a 1 for AMH and a 2 for BHC since those are the factor levels that R would automatically assign. However, I’m not sure how to do this, or if it is possible.

  • 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-14T00:44:32+00:00Added an answer on June 14, 2026 at 12:44 am

    I would use this command:

    nData <- within(nData, compLbl2 = rev(as.numeric(compLbl[drop = TRUE]) -1))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have DataSet which has 3 columns. Name - insurance comp. name - treatmentDate
i have C# program, i have DataSet that i want to bind to crystal
I have a DataSet which has one table loaded with data. When i write
I have a DataSet which I get a DataTable from that I am being
I have a dataset that has two tables in it. I want to do
I have a DataSet with a DataTable that correctly fills a single DataRow through
I have a dataset that is has a number of groups. I have to
I have a dataset that i am trying to set the default value as
I have a dataset with multiple tables. One table in particular has one record
I have a DataSet with some DataTables that are linked together with DataRelations (classic

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.