How do I apply a patch to a data.frame file?
For lack of a better word, how can I apply a "patch" to the R data.frame? Suppose I have a master database with branded and output columns and variable ownership, which is 1 or 0 in this example, but can 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 firm "one" to retail "1" branded "red" which transaction I have in another data.frame
// delta
firm outlet shares.delta
1 one 1 -1
2 red 1 1
What's the most efficient way in R to apply this "patch" or transaction to my main 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 don't really recommend storing the suffixes pre
, post
or delta
. If they were all named shares
, that would be nice too, I just want to "add" these data frames.
UPDATE : my current approach is
update <- (master$firm %in% delta$firm) & (master$outlet %in% delta$outlet)
master[update,]$shares <- master[update,]$shares + delta$shares
Yes, I know that it does vector scanning to create a Boolean vector update
and that the subset is not very efficient either. But what I hate the most is that I have to write down the appropriate columns.
source to share
Another way: data.table
. Assuming you have loaded both your data into df1
and df2
data.frame
s,
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
source to share
I would give a more accurate answer if you provided a reproducible example , but here's one way:
- Call your first data.frame
dat
and secondchg
Then you can combine the two:
dat <- merge(dat,chg)
And just subtract:
dat$shares <- with(dat, shares.pre + shares.delta )
source to share