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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:58:45+00:00 2026-06-13T11:58:45+00:00

Before I end up at -7 like Two by two matching between dataframes in

  • 0

Before I end up at -7 like Two by two matching between dataframes in r did, let me say I’ve already read the following pages:

  • How to copy row from one data.frame in to another [R]
  • R How to update a column in data.frame using values from another data.frame
  • Using one data.frame to update another

Actually, the last one is really similar to what I want, but not the same, because my columns are different

I have two dataframes, let’s say:

> d <- data.frame(year=c(2004,2004,2006),month = c(1,5,3), height = c(1000,2000,3000) )
> d
  year month height
1 2004     1   1000
2 2004     5   2000
3 2006     3   3000

> e <- data.frame(year=c(2004),month=c(5), height = c(9999))
> e
  year month height
1 2004     5   9999

Obviously the real data is longer than this.

I want to merge the values from e into d

Try raw merge:

> merge(d,e)
[1] year   month  height
<0 rows> (or 0-length row.names)

Ok. So add “by”:

> merge(d,e,by=c("year","month"))
  year month height.x height.y
1 2004     5     2000     9999

Ok, it did an inner join, and got rid of all the original data in d. So try left outer join:

> merge(d,e,by=c("year","month"),all.x = T)
  year month height.x height.y
1 2004     1     1000       NA
2 2004     5     2000     9999
3 2006     3     3000       NA

It did a join, and it’s correct as per outer-join definition, but it didn’t do what I want, which is to update the values in d from the values in e. What I really want is more like an sql update:

for (year,month,height) in e:
    update d set d.height=e.height where d.year = e.year and d.month = e.month

ie the results I want would be:

> magic(d,e)
  year month height
1 2004     1   1000
2 2004     5   9999
3 2006     3   3000

Of course, I could just write a bunch of for loops, but I’m hoping there is some vectorized way of doing this?

Edit: my example had only one key column, but my real problem has two. Updated the example to reflect this.

  • 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-13T11:58:46+00:00Added an answer on June 13, 2026 at 11:58 am

    Actually, the following approach is more straightforward:

    rownames( d ) <- d$id
    d[ e$id, ]$height <- e$height
    

    Update: since your key is actually “year-month”, you probably will be best off with the data table, but if you are not willing using that, here is what you can do:

    rownames( d ) <- paste( d$year, d$month )
    d[ paste( e$year, e$month ), ]$height <- e$height
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let’s say you wish to have two classes like so, each declaring an instance
I'm getting an end of file before the left brace was matched while trying
I want to get one record before start date and end date DtpFrom means
I've never come across this extension before. It's being used for a front-end interface
Please look at this code: @interface myObject:NSObject -(void)function:(id)param; @end @implementation myObject -(void)function:(id)param { NSLog(@BEFORE);
I have two tables that look like this: node: NID Name 1 Chicago 2
I feel like this question has been asked before but the answers seem pretty
Using six-bit one's and two's complement representation I am trying to solve the following
I have declared my two dimensional array like this. But getting an error due
Two similar questions have been asked here before: Split view in portrait mode! Forcing

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.