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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:42:51+00:00 2026-05-26T21:42:51+00:00

I have a dataframe like this: V1 V2 V3 1 1 3423086 3423685 2

  • 0

I have a dataframe like this:

  V1      V2      V3 
1  1 3423086 3423685 
2  1 3467184 3467723 
3  1 4115236 4115672 
4  1 5202437 5203057 
5  2 7132558 7133089 
6  2 7448688 7449283 

I want to change the V1 column and add chr before the number. Just like this:

  V1      V2      V3 
1  chr1 3423086 3423685 
2  chr1 3467184 3467723 
3  chr1 4115236 4115672 
4  chr1 5202437 5203057 
5  chr2 7132558 7133089 
6  chr2 7448688 7449283 

Is there a way to do this in R?

  • 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-26T21:42:52+00:00Added an answer on May 26, 2026 at 9:42 pm

    The regex pattern “^” (outside any character-class brackets) represents the point just before the first character of a “character”-class item (aka “string” in other computer languages). This just replaces the beginning of each “character” element in vector with a stem of “chr”. It implicitly coerces a “numeric” input value to “character” so alters the mode of the result.

    > dat$V1 <- sub("^", "chr", dat$V1 )
    > dat
        V1      V2      V3
    1 chr1 3423086 3423685
    2 chr1 3467184 3467723
    3 chr1 4115236 4115672
    4 chr1 5202437 5203057
    5 chr2 7132558 7133089
    6 chr2 7448688 7449283
    

    Could, of course, have used paste("chr", dat$V1, sep=""), but I thought a regex solution might be neater.

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

Sidebar

Related Questions

Suppose I have a dataframe like this one: df <- data.frame (id = c(a,
I have a dataframe that looks like this: person n start end 1 sam
I have a dataframe with numeric entries like this one test <- data.frame(x =
If you have a dataframe like this mydf <- data.frame(firstcol = c(1,2,1), secondcol =
I have a dataframe like this: block plot date data 1 1 aug 11.95171507
I have a column in a dataframe like this: npt2$name # [1] Andreas Groll,
Say I have a dataframe source <- data.frame(ID=c(1,2), COUNT=c(3,4)) that looks like this: ID
I have a big dataframe, but small example would be like this: mydf <-
I have a input dataframe like this (the real one is very large, so
I have a dataframe that looks like this DF: V1 V2 V3 V4 V5

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.