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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:23:39+00:00 2026-05-27T07:23:39+00:00

These are the definitions of df1 and df2: df1 <- data.frame(x = 1:3, y=letters[1:3])

  • 0

These are the definitions of df1 and df2:

df1 <- data.frame(x = 1:3, y=letters[1:3])
df2 <- data.frame(x= rep(c(1,2,3),each=3))

I want to assign the value of column y in df1 to column y in df2, where the value in column x of df1 is equal to the value in column x of df2. As shown above df1 and df2 are of unequal length.

for(i in 1:length(df2$x)){
        df2$y[i]<- df1$y[which(df1$x == df2$x[i])]
}

I am not looking for short cuts to do this (no builtin functions please). I want to learn it the right way.

Is my logic correct?
If it is why is this not working?

Any guidance will be highly 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-05-27T07:23:40+00:00Added an answer on May 27, 2026 at 7:23 am

    Taking what you call “shortcuts” is actually the right way to do things in R. But I do think that looping through manually is sometimes a good exercise. But in your “production code”, ie code that you want to count on, use the built-in functions when they are applicable.

    You’re just missing one option for your data.frame. Everything else is fine. The problem is that by default, character vectors are input as factors in a data.frame and when you try to replace a value with a value from a factor vector it is replacing it with the underlying numeric index of that level. Here is the complete code:

    df1 <- data.frame(x = 1:3, y=letters[1:3], stringsAsFactors=FALSE)
    
    df2 <- data.frame(x= rep(c(1,2,3),each=3))
    
    for(i in 1:length(df2$x)){
    
        df2$y[i]<- df1$y[which(df1$x == df2$x[i])]
    }
    df2
      x y
    1 1 a
    2 1 a
    3 1 a
    4 2 b
    5 2 b
    6 2 b
    7 3 c
    8 3 c
    9 3 c
    

    See ?data.frame for more info on the stringsAsFactors option

    Since you seem interested in learning, here’s a way you might have gone about debugging. Suppose your original commands are in a file called temp.R. Then

    > source('temp.R')
    > ls()
    [1] "df1" "df2" "i"
    

    i is left over after the for loop. Let’s use it so that your following commands with i in them will work. You can reassign a value to i to see what your command would give for other values. Now lets start breaking your code down to see where the problem is.

    > i
    [1] 9
    > which(df1$x == df2$x[i])
    [1] 3
    

    Looks good so far. 3 is what we expect it to be, right?

    > df1$y[which(df1$x == df2$x[i])]
    [1] c
    Levels: a b c
    

    Here is where you need to recognize “oh, this is a factor!”. Whenever you see “Levels” the “factor” lightbulb should be lighting up in your head.

    Let’s see the value before we try the replacement just to be sure the rest of your code didn’t accidentally modify it:

    > df2$y[9]
    [1] 3
    

    Looks good. We know what happens after the replacement, so clearly something goes wrong with the assignment. Let’s try this just to see what happens:

    > df2$y[9] <- as.factor("c")
    > df2$y[9]
    [1] 1
    

    Clearly something is wrong. Thus we’ve narrowed the problem down to here. Now we need to go back up to find out why we’re replacing with a factor. Hopefully that will lead you to the data.frame help.

    Things like this are annoying in R but you just have to have faith that there are reasons for behavior like this, and once you learn more coding in R and more of R‘s philosophy, you won’t have as many surprises such as this. Good luck!

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

Sidebar

Related Questions

In the following table definition, what is the difference between these two column definitions,
I have a code base, in which for Matrix class, these two definitions are
In my table definition, there is a column with int data type. If a
These days I design some algorithms in python, but find first two greatest value
What's the difference between these two definitions?: def sayTwords(word1: String, word2: String) = println(word1
WCF generates complex types as external xsd files. How can I embed these definitions
I need help with parameteres. Do both of these function definitions do the exact
If I have these declarations and definitions: enum Events { INIT, RENDER }; struct
Some of these Preprocessor definitions are in the WinMain function and other windows library
What do *args and **kwargs mean in these function definitions? def foo(x, y, *args):

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.