r - Filter rows below a given row element within a group -
I have a datafile with one set of events dated in each row linked from one place to another Index is a series of events and different matches that could have been done after the first and / or index events. Every place needs to reduce all the events occurring before the index event. I data structure looks like this.
Localized match date score iid 1 index 4/11/2013 15 1 1 Milan 1/09/2013 23 2 1 Milan 14/04/2013 1 3 1 Milan 7/1/2014 21 4 2 Index 2/4/2013 12 1 2 Milan 1/2/2013 10 2 3 Index 1/5/2013 23 1 3 Milan 2/5/2013 10 2 4 Index 3/3/2013 9 1 4 Milan 10 / 2/2013 32 2 4 Match 1/10/2012 15 3 4 Match 4/3/2013 12 4 4 Milan 10/3/2013 10 5
And to reduce dataframe Is required so that each location needs to end with the rows below the date of the index event.
localized match date score iid 1 matched 1/09/2013 2013 23 2 1 match 14 / 04/2013 1 3 2 Milan 1/2/2013 10 2 4 Milan 10/2/2013 32 2 4 Milan 1/10/2012 15 3 I ask here for the first time, so I hope that I am not doing it wrong. I tried different versions of the solution within R, but I am struggling to find the right search.
Here is a data.table
probability ( df
Library (data capable) setDT (df) [, Date: = As.Date (date, format = "% d /% m /% Y to handle your data. ")] [,. SD [date & lt; Date [match == "index"]], by = Lloyd # Lloyd match date score IID # 1: 1 matched 2013-09-01 23 2 # 2: 1 matched 2013-04-14 1 3 # 3 : 2 matched 2013-02-01 10 2 # 4: 4 Match 2013-02-10 32 2 # 5: 4 Milan 2012-10-01 15 3
Potential Basis R Solutions
df & lt; - conversion (df, date = as.Date (date = "% d /% m /% Y")) doall (rbind, by (df, df) $ locid, fune = function (x) x [with ( X, date & date; [match == "index"]),]) # locid match date score iid # 1.2 1 Match 2013-09-01 23 2 # 1.3 1 Macd 2013-04-14 1 3 # 2 matched 2013-02-01 10 2 # 4.10 4 matches 2013-02-10 32 2 # 4.11 4 Milan 2012-10-01 15 3
And another possible basis R solution
df < - transform (df, date = as.Date (date, format = "% d /% m /% Y") (), ((x, date and lieutenant; date [match == "index"]), ]) # Date of match of Lloyd match IID # 1.2 1 Macked 2013-09-01 23 2 # 1.3 1 Macked 2013-04-14 1 3 # 2 2 Match 2013-02-01 10 2 # 4.10 4 Match 2013-02 -10 32 2 # 4.11 4 matched 2012-10-01 15 3
Here the original idea is to have your date
column in The date is changed so that the class will be able to identify its command. Later, we basically use the data by locid
and Split and apply a filtering function on each segment which only selects the date that comes before date
match == index
Comments
Post a Comment