Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting rid of wash out rows in R dataframe by group

This is what my dataframe looks like:

df <- read.table(text='
    CustomerName    Sales          TradeDate
    John           1000              1/1/2015
    John          -1000              1/1/2015
    John           1000              1/1/2015
    John           5000              2/1/2015
    John          -2000              3/1/2015
    John           2000              3/2/2015
    John           2000              3/3/2015
    John          -2000              3/4/2015
    John           2000              3/5/2015
    John           2000              3/6/2015
    John          -3000              4/1/2015
    John           3000              4/1/2015
    John          -3000              4/1/2015
    John           2000              5/1/2015
    John          -2000              5/1/2015
    John           2000              5/1/2015
    Tom            1000              1/1/2015
    Tom           -1000              1/1/2015
    Tom            1000              1/1/2015
    Tom            5000              2/1/2015
    Tom           -2000              3/1/2015
    Tom            2000              3/1/2015
    Tom           -2000              3/1/2015
    Tom            2000              3/1/2015
    Tom            2000              3/1/2015
    Tom           -3000              4/1/2015
    Tom            3000              4/1/2015
    Tom           -3000              4/1/2015
                                             ', header=T)

I want to get rid of all the Sales that are equal in amount and opposite in sign(+,-)and only show the remaining net Sales(preferably at the earliest date, but it does not matter either way). My desired data frame looks like this

CustomerName    Sales   TradeDate
John            1000    1/1/2015
John            5000    2/1/2015
John            2000    3/3/2015
John            2000    3/6/2015
John           -3000    4/1/2015
John            2000    5/1/2015
Tom             1000    1/1/2015
Tom             5000    2/1/2015
Tom             2000    3/1/2015
Tom            -3000    4/1/2015

I chose two 2000s(in John's case in the month of March) from 3/3/2015 and 3/6/2015. But I am also okay with an output that gave me two 2000s at 3/2/2015 or 3/5/2015. Your help is much appreciated!

like image 577
gibbz00 Avatar asked Dec 05 '25 05:12

gibbz00


1 Answers

Here's what I would do, in data.table:

library(data.table)

# identify how many transactions we need to keep
setDT(df)[,
    n_keep := sum(Sales)/transval
,by=.(CustomerName,transval=abs(Sales))]

# tag those transactions
df[sign(Sales)==sign(n_keep),
    keep := 1:.N %in% tail(1:.N,abs(n_keep[1]))
,by=.(CustomerName,Sales)]

# keep 'em
df[(keep)][,c("n_keep","keep"):=NULL][]

which gives

   CustomerName Sales TradeDate
1:         John  1000  1/1/2015
2:         John  5000  2/1/2015
3:         John  2000  3/5/2015
4:         John  2000  3/6/2015
5:         John -3000  4/1/2015
6:          Tom  1000  1/1/2015
7:          Tom  5000  2/1/2015
8:          Tom  2000  3/1/2015
9:          Tom -3000  4/1/2015

I'm sure my code could be simplified, but I think the steps are pretty transparent.

like image 55
Frank Avatar answered Dec 07 '25 20:12

Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!