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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:52:48+00:00 2026-05-22T17:52:48+00:00

I have a data frame ( all_data ) in which I have a list

  • 0

I have a data frame (all_data) in which I have a list of sites (1… to n) and their scores e.g.

  site  score
     1    10
     1    11  
     1    12
     4    10 
     4    11
     4    11
     8    9
     8    8
     8    7

I want create a column that numbers each level of site in numerical order, like a counter. In the example, the sites (1, 4, and 8) would be have a corresponding counter from 1 to 3 in the ‘number’ column:

site  score number
     1    10    1
     1    11    1 
     1    12    1 
     4    10    2
     4    11    2
     4    11    2
     8    9     3
     8    8     3 
     8    7     3

I am sure this must be easily solved, but I have not found a way yet.

  • 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-22T17:52:49+00:00Added an answer on May 22, 2026 at 5:52 pm

    Try Data$number <- as.numeric(as.factor(Data$site))

    On a sidenote : the difference between the solution of me and @Chase on one hand, and the one of @DWin on the other, is the ordering of the numbers. Both as.factor and factor will automatically sort the levels, whereas that doesn’t happen in the solution of @DWin :

    Dat <- data.frame(site = rep(c(1,8,4), each = 3), score = runif(9))
    
    Dat$number <- as.numeric(factor(Dat$site))
    Dat$sitenum <- match(Dat$site, unique(Dat$site) ) 
    

    Gives

    > Dat
      site     score number sitenum
    1    1 0.7377561      1       1
    2    1 0.3131139      1       1
    3    1 0.7862290      1       1
    4    8 0.4480387      3       2
    5    8 0.3873210      3       2
    6    8 0.8778102      3       2
    7    4 0.6916340      2       3
    8    4 0.3033787      2       3
    9    4 0.6552808      2       3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data.frame in R that looks like this: score rms template aln_id
I am trying create a data.frame from which to create a graph. I have
I have a data.frame , called so_data. Columns 13:23 are list s, which hold
I have a data frame with two columns. First column contains categories such as
I have a data.frame called series_to_plot.df which I created by combining a number of
I have a data frame where one particular column has a set of specific
I have a data frame in R that has come about from running some
I have a data.frame in R which has been constructed off the Example 1-3
SQLDF newbie here. I have a data frame which has about 15,000 rows and
I have a data frame with 3 variables, which are all wind speeds. 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.