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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:03:15+00:00 2026-05-19T16:03:15+00:00

I have 2 columns of data with the same type of data (Strings). I

  • 0

I have 2 columns of data with the same type of data (Strings).

I want to join the levels of the columns. ie. we have:

col1   col2
Bob    John
Tom    Bob
Frank  Jane
Jim    Bob
Tom    Bob
...    ... (and so on)

now col1 has 4 levels (Bob, Tom Frank, Jim) and col2 has 3 levels (John, Jane, Bob)

But I want both columns to have all the factor levels (Bob, Tom, Frank, Jim, Jane, John), as to later replace each of the ‘names’ with a unique id, such that the final output would be:

col1   col2
1      5
2      1
3      6
4      1
2      1

that is Bob -> 1, Tom -> 2, etc. in both columns.

Any ideas 🙂 ?

edit: Thanks all for the wonderful answers! You are all awesome as far as I know 🙂

  • 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-19T16:03:16+00:00Added an answer on May 19, 2026 at 4:03 pm

    You want the factors to include all the unique names from both columns.

    col1 <- factor(c("Bob", "Tom", "Frank", "Jim", "Tom"))
    col2 <- factor(c("John", "Bob", "Jane", "Bob", "Bob"))
    mynames <- unique(c(levels(col1), levels(col2)))
    fcol1 <- factor(col1, levels = mynames)
    fcol2 <- factor(col2, levels = mynames)
    

    EDIT: a little nicer if you replace the third line with this:

    mynames <- union(levels(col1), levels(col2))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a LINQ to SQL class called Data with a column of type
I have copied the exact same code of the grid paging example from the
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I've got a column called description of type NVARCHAR(MAX) - biggest you can have.
I am designing a password recovery program, and I want to make it right/modular
Is it safe to change VirtualTreeView data from the secondary thread ? And if
I have extended the Entity framework to implement an interface by adding to the
i don't even know if calling it serialized column is right, but i'm going
I am starting to work with Nhibernate and Oracle for a project. The database
I'm trying to store images in a sql server database. I've got a column

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.