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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:41:12+00:00 2026-05-16T23:41:12+00:00

I have a dataframe in R with a vector of non-sequential numbers ( data$SiteID

  • 0

I have a dataframe in R with a vector of non-sequential numbers (data$SiteID) that i would like to map to a vector of sequential numbers (data$site) to the unique values of data$SiteID. Within each site, I would like to map data$TrtID to 0 where data$TrtID == 'control' or to the next sequential integer, for the other unique data$TrtID‘s:

data <- data.frame(SiteID = c(1,1,1,9,'108','108','15', '15'), 
                   TrtID = c('N', 'control', 'N', 'control', 'P', 'control', 'N', 'P'))
  1. data$site should be c(1,1,1,2,3,3,4,4).
  2. data$trt should be c(1,0,1,0,1,0,0,1).
  • 1 1 Answer
  • 1 View
  • 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-16T23:41:13+00:00Added an answer on May 16, 2026 at 11:41 pm

    Use conversion of factors to integers:

    transform(data, site=as.integer(SiteID), trt=as.integer(TrtID))
    

    If the ordering is important, you can give specific orders to the levels:

    transform(data,
      site = as.integer(factor(SiteID, unique(SiteID))),
      trt  = as.integer(factor(TrtID, unique(c('control', as.character(TrtID))))) - 1L)
    

    Modified version grouping trt factor by site:

    transform(data,
      site = as.integer(factor(site_id, unique(site_id))),
      trt  = unsplit(tapply(trt_id, site_id, function(x)
             as.integer(factor(x))), site_id) - 1L)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataframe with a column of integers that I would like to
I have a dataframe and would like to calculate the correlation (with Spearman, data
I have a dataframe with distances df<-data.frame(site.x=c(A,A,A,B,B,C), site.y=c(B,C,D,C,D,D),Distance=c(67,57,64,60,67,60)) I need to convert this to
I have a dataframe with numeric entries like this one test <- data.frame(x =
I have a dataframe and would like to add a new column where the
Suppose I have a data frame, df, that looks like: f t1 t2 t3
i have the following conundrum. I have a dataframe x that looks like: v2
I have big data frame with various numbers of columns and rows. I would
I have a dataframe x with this values: x1 x2 x3 1 NA 4
I have a dataframe called split2_data (actually a drop-leveled subset of a bigger data

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.