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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:02:01+00:00 2026-06-05T14:02:01+00:00

I have two data frames, one with 1 column (X), and the other with

  • 0

I have two data frames, one with 1 column (X), and the other with 2 columns (Y & Z):

Column X contains numbers 1:99, but occasionally has some letters instead of numbers, ie: 1, 2, 3, A, 5, B, 7, 8, C, D, 11, 12 etc.

Column Y contains these same letters, which are paired (as appearing in column Z) to certain numbers, ie:

A 4

B 6

C 9

D 10

How can I replace the letters in column X with the values of column Z, according to whether the letters in column X match with the letters in column Y? This would result in column X being 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 etc.

A straightforward merge won’t work (I need to retain all values in X) and I’m not sure how I can use sub conditionally. Also, column Y and Z contain more rows than needed for column X, so I can’t just use cbind. I’m not very skilled at using regex, although that is probably my best bet…

Any help would be greatly appreciated!

  • 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-06-05T14:02:04+00:00Added an answer on June 5, 2026 at 2:02 pm

    I’d just use a for loop. Creating your data:

    df1 <- data.frame(X = c("A", 5, "B", 7, 8, "C", "D", 11, 12))
    df2 <- data.frame(Y = c("A", "B", "C", "D"),
                      Z = c(4, 6, 9, 10))
    

    We need to make sure things are character vectors, not factors, for testing equality

    df1$X <- as.character(df1$X)
    df2$Y <- as.character(df2$Y)
    

    Then we can do the replacing:

    for (i in 1:nrow(df2)) {
        df1$X[df1$X == df2$Y[i]] <- as.character(df2$Z[i])
    }
    

    Finally, I’m guessing you want the X as numeric now that all the letters are gone:

    df1$X <- as.numeric(df1$X)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two data.frames in R, one of which has two columns and of
I have two data frames with two columns each. The first column is timestamps
I have a data frame with two columns. First column contains categories such as
I have two data.frames, one with only characters and the other one with characters
Right now I have two data frames in R, contains some data that looks
Let's say I have two data frames. Each has a DAY, a MONTH, and
I have the following matching problem: I have two data.frames, one with an observation
I have two data frames. The one consists of three variables, namely date, strike
I have two R data frame with differing dimensions. However but data frames have
I have a data frame with two columns. The first column defines subsets of

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.