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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:37:58+00:00 2026-05-25T12:37:58+00:00

I have two data.frames as follows: df1 <- data.frame(A=c(lee,eeu,ees), B=c(lee,ggu,1su), C=c(1,1,1) A B C

  • 0

I have two data.frames as follows:

df1 <- data.frame(A=c("lee","eeu","ees"), B=c("lee","ggu","1su"), C=c(1,1,1)

    A   B C
1 lee lee 1
2 eeu ggu 1
3 ees 1su 1


df2 <- data.frame (X=c("lee","1su","eeu","ggu"), Y=c("3k3","4k4","5k","2ee"), Z=c("ggg","","","ooo"), ZA=c("vvv","","",""))

    X   Y   Z  ZA
1 lee 3k3 ggg vvv
2 1su 4k4        
3 eeu  5k        
4 ggu 2ee ooo    

I want to expand df1 by matching df1$B with df2$X. When df1$B = df2$X, I want to add additional rows to the new_df1 with new B = other entries in df2 on the same row, but keeping A and C the same.

new_df1 is expected to be as follows:

 A   B  C
lee 3k3 1 ### df1$B1= df2$X1= lee
lee ggg 1
lee vvv 1
eeu 2ee 1 ### df1$B2= df2$X4= ggu
eeu ooo 1
ees 4k4 1 ### df1$B3= df2$X2= lsu

My past experience on using lapply seems to be very memory-demanding, is it possible to be done without using lapply?

  • 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-25T12:37:58+00:00Added an answer on May 25, 2026 at 12:37 pm

    I think what you wnat is a subset of this:

    require(reshape2)
    merge(df1,melt(df2, id.var="X"), by.x="B", by.y="X", all=TRUE)
         B    A  C variable value
    1  1su  ees  1        Y   4k4
    2  1su  ees  1        Z      
    3  1su  ees  1       ZA      
    4  ggu  eeu  1        Y   2ee
    5  ggu  eeu  1        Z   ooo
    6  ggu  eeu  1       ZA      
    7  lee  lee  1        Y   3k3
    8  lee  lee  1        Z   ggg
    9  lee  lee  1       ZA   vvv
    10 eeu <NA> NA        Y    5k
    11 eeu <NA> NA        Z      
    12 eeu <NA> NA       ZA      
    

    I assigned that object to “M1” (and later noticed that it did not need all=TRUE)

    M1 <- merge(df1,melt(df2, id.var="X"), by.x="B", by.y="X")
    subset(M1, value != "" , select=c(A,value, C) )
        A value C
    1 ees   4k4 1
    4 eeu   2ee 1
    5 eeu   ooo 1
    7 lee   3k3 1
    8 lee   ggg 1
    9 lee   vvv 1
    
    • 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 DF1, DF2 each with 2 vectors DF1$A DF1$B, DF2$C,
I have two data.frames. For examples sake let's say they look like this: df1
Given two data frames: df1 = data.frame(CustomerId = c(1:6), Product = c(rep(Toaster, 3), rep(Radio,
I have two R data frame with differing dimensions. However but data frames have
I have a data frame with two columns. First column contains categories such as
I have two data frames: A and B of the same number of columns
I have two small data frames, this_tx and last_tx . They are, in every
Let's say I have two data frames. Each has a DAY, a MONTH, and
Is it possible to row bind two data frames that don't have the same
I have the following matching problem: I have two data.frames, one with an observation

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.