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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:37:12+00:00 2026-05-13T22:37:12+00:00

I have a data frame with gaps like this: Var1 Var2 Var3 1 NA

  • 0

I have a data frame with gaps like this:

           Var1    Var2    Var3
1            NA      NA      NA
2            NA      NA      NA
3            NA      NA      NA
4            NA 0.06703      NA
5            NA 0.08639      NA
6            NA 0.19023 0.02322
7            NA 0.31764 0.08058
8            NA 0.44426 0.15081
9            NA 0.37529 0.29595
10           NA 0.40029 0.29274
11           NA 0.33828 0.39168
12      0.01595 0.31432 0.43192
13      0.05217 0.28560 0.48150
14      0.07196 0.32588 0.56065
15      0.08771 0.26301 0.68131

When I run melt(), I remove the NA cells:
melt(df, na.rm = TRUE), but I would like to add a new column that contains a row number for each value’s position within its variable group.

So my results look like this:

variable    value
    Var1   0.01595
    Var1   0.05217
    Var1   0.07196
    Var1   0.08771
    Var2   0.06703
    Var2   0.08639
...etc

and I want them to look like this:

variable    value    index
    Var1   0.01595   1
    Var1   0.05217   2
    Var1   0.07196   3
    Var1   0.08771   4
    Var2   0.06703   1
    Var2   0.08639   2
...etc

What’s the best way to generate these internal row numbers, whether before, during, or after the melt process?

  • 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-13T22:37:12+00:00Added an answer on May 13, 2026 at 10:37 pm

    Take a look at this previous question about auto incremented cohort counts. I think this is what you are wanting to do. If so, probably easiest (at least for me) to do this as a separate operation with plyr.

    Here’s the gist:

    ddply(df, .(variable), function(x) data.frame(x, NewID=1:nrow(x))) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
Let say I have the following desire, to simplify the IConvertible's to allow me
I am attempting to pull some information from my tnsnames file using regex. I

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.