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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:46:32+00:00 2026-06-18T00:46:32+00:00

For lack of a better word, how do I apply a patch to a

  • 0

For lack of a better word, how do I apply a “patch” to a R data.frame? Suppose I have a master database with firm and outlet columns and an ownership shares variable that is 1 or 0 in this example, but could be any percentage.

// master
     firm outlet shares.pre
1    five      1          0
2     one      1          1
3     red      1          0
4  yellow      1          0
5    five      2          0
6     one      2          0
// many more

I want to let firm “one” sell outlet “1” to firm “red”, which transaction I have in another data.frame

// delta
  firm outlet shares.delta
1  one      1         -1
2  red      1          1

What is the most efficient way in R to apply this “patch” or transaction to my master database? The end result should look like this:

// preferably master, NOT a copy
     firm outlet shares.post
1    five      1          0
2     one      1          0  <--- was 1
3     red      1          1  <--- was 0
4  yellow      1          0
5    five      2          0
6     one      2          0
// many more

I am not particular about keeping the suffixes pre, post or delta. If they were all named shares that would be fine too, I simply want to “add” these data frames.

UPDATE: my current approach is this

update <- (master$firm %in% delta$firm) & (master$outlet %in% delta$outlet)
master[update,]$shares <- master[update,]$shares + delta$shares

Yes, I’m aware it does a vector scan to creat the Boolean update vector, and that the subsetting is also not very efficient. But the thing I don’t like about it most is that I have to write out the matching columns.

  • 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-18T00:46:33+00:00Added an answer on June 18, 2026 at 12:46 am

    Another way using data.table. Assuming you’ve loaded both your data in df1 and df2 data.frames,

    require(data.table)
    dt1 <- data.table(df1)
    dt2 <- data.table(df2)
    setkey(dt1, firm, outlet)
    setkey(dt2, firm, outlet)
    dt1 <- dt2[dt1]
    dt1[is.na(dt1)] <- 0
    dt1[, shares.post := shares.delta + shares.pre]
    
    #      firm outlet shares.delta shares.pre shares.post
    # 1:   five      1            0          0           0
    # 2:   five      2            0          0           0
    # 3:    one      1           -1          1           0
    # 4:    one      2            0          0           0
    # 5:    red      1            1          0           1
    # 6: yellow      1            0          0           0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data that isn't properly partitioned (for lack of a better word).
I have a small issue(for lack of a better word) with MySQL db. I
I have a list of paths (for lack of a better word, maybe bread
This seems so basic, I'm flabbergasted for lack of a better word. I have
So I have noticed that things (for lack of a better word) like this
Is there such a thing as a translator (for lack of a better word
On my web page, I have multiple independent plug-ins (for lack of a better
When I click End, it doesn't destroy (lack of a better word) the setTimeout
I have an appliance (for lack of better description) running linux. Currently I ssh
Is there support in Ruby for (for lack of a better word) non-escaped (verbatim)

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.