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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:46:52+00:00 2026-05-25T14:46:52+00:00

I would like to combine three columns in one of my data sets into

  • 0

I would like to combine three columns in one of my data sets into one with variable name “al_anim” and remove any duplicates, rank the values (animal ids) from lowest to highest, and re-number each animal from 1 to N under the variable name “new_id”.

 anim1 <- c(1456,2569,5489,1456,4587)
 anim2 <- c(6531,6987,6987,15487,6531)
 anim3 <- c(4587,6548,7894,3215,8542)
 mydf <- data.frame(anim1,anim2,anim3)

Any help would be very much appreciated!

Baz

  • 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-25T14:46:52+00:00Added an answer on May 25, 2026 at 2:46 pm

    Using mydf from your example:

    mydf <- data.frame(anim1, anim2, anim3)
    

    Stack the data:

    sdf <- stack(mydf)
    

    Then compute the unique elements using unique()

    uni <- unique(sdf[, "values"])
    

    and then this will get them a new animal id

    new_id <- as.numeric(as.factor(sort(uni)))
    

    which would give:

    > new_id
     [1]  1  2  3  4  5  6  7  8  9 10 11
    

    However that is totally trivial; seq_along(uni) gets you there far more easily. So I wonder if you want

    newdf <- data.frame(anim = sort(uni), new_id = seq_along(uni))
    merge(sdf, newdf, by.x = "values", by.y = "anim")
    

    which gives:

    > merge(sdf, newdf, by.x = "values", by.y = "anim")
       values   ind new_id
    1    1456 anim1      1
    2    1456 anim1      1
    3    2569 anim1      2
    4    3215 anim3      3
    5    4587 anim1      4
    6    4587 anim3      4
    7    5489 anim1      5
    8    6531 anim2      6
    9    6531 anim2      6
    10   6548 anim3      7
    11   6987 anim2      8
    12   6987 anim2      8
    13   7894 anim3      9
    14   8542 anim3     10
    15  15487 anim2     11
    

    There is an amount of ambiguity in your Question which could be alleviated by giving an expected result/output.

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

Sidebar

Related Questions

I would like to combine these four queries into one: SELECT COUNT(ID) FROM conversations
I have two views that I would like to combine into one. The first
I have these 2 queries that I would like to combine into one big
I have multiple datasets that I would like to combine into one. There is
I would like to combine two __m128 values to one __m256 . Something like
I would like to match two files based on one column and combine the
This is the sequel to this question . I would like to combine three
I am trying to combine three Byte values to one Long value, like System.Drawing.Color.ToArgb()
I have a variable number of divs acting as columns, and I would like
I would like to combine the DisplayNames from two different ViewModels, but only IF

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.