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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:12:12+00:00 2026-06-16T18:12:12+00:00

I have two data frames with the same 3 columns: WeekNum, Year and Number.

  • 0

I have two data frames with the same 3 columns: WeekNum, Year and Number.

A <- data.frame(WeekNum=c(1,2,3,4,5,1,2,3,4,5), 
                Year=c(2000,2000,2000,2000,2000,2001,2001,2001,2001,2001), 
                Number=c(0,0,0,0,0,0,0,0,0,0))

B <- data.frame(WeekNum=c(1,2,3,4,1,2,6), 
                Year=c(2000,2000,2000,2000,2001,2001,2001), 
                Number=c(0,1,0,1,2,5,6))

I want to create a new data frame with the same 3 columns using all WeekNum and Year combinations from A (and only those from B that are also present in A). When a WeekNum and Year combination is also present in B, I want to use the Number value from B. If the combination is not present in B, I want to leave the Number value as 0. Ultimately, I should have a data frame that looks like:

> C
   WeekNum Year Number
1        1 2000      0
2        2 2000      1
3        3 2000      0
4        4 2000      1
5        5 2000      0
6        1 2001      2
7        2 2001      5
8        3 2001      0
9        4 2001      0
10       5 2001      0
  • 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-16T18:12:14+00:00Added an answer on June 16, 2026 at 6:12 pm

    A easy way is to create an id column for both data.frames A and B and then use match:

    A$id <- paste(A$WeekNum, A$Year, sep=":")
    B$id <- paste(B$WeekNum, B$Year, sep=":")
    
    m1 <- match(A$id, B$id)
    m2 <- which(!is.na(m1))
    A$Number[m2] <- B$Number[m1[!is.na(m1)]]
    subset(A, select=-c(id))
    

    output: (Edit: I see you changed your input data.frame)

       WeekNum Year Number
    1        1 2000      0
    2        2 2000      1
    3        3 2000      0
    4        4 2000      1
    5        5 2000      0
    6        1 2001      2
    7        2 2001      5
    8        3 2001      0
    9        4 2001      0
    10       5 2001      0
    
    • 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: A and B of the same number of columns
While data frame columns must have the same number rows, is there any way
I have two dataframes with the same number of columns. I'm writing a function
I have two data frames with two columns each. The first column is timestamps
I have two columns in a data frame, and I have been able to
I have a data frame with two columns. First column contains categories such as
Is it possible to row bind two data frames that don't have the same
I have a various data.frames with columns of the same length where I am
Suppose I have two data frame 'df_a' & 'df_b' , both have the same
I have a simple dataframe with two columns: df <- data.frame(x = c(1,1,2,2,3), y

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.