Omit outdated records after adding amended records

In the juvenile court judges often have the power to amend charges applied to a case. A charge of “INTENT TO DISTRIBUTE” may, for instance, be reduced to something less severe, say “POSSESSION OF PARAPHERNALIA.” When this occurs, a new charge is added to a case record and the old charge is, in some cases, retained. This short post presents R code to omit outdated charges after amended charges have been added.

Data for this example consist of charges applied to a single court record. These data are provided below:

charge=c("Count 1", "Count 2", "Amended", "Count 3", "Amended")
section=c("21-5807(a)(3)", "21-5807(a)(3)", "21-5801(b)(4)", "21-5807(a)(3)", "21-5801(b)(4)")
date=c("09/13/15", "09/20/15", "04/04/16", "10/03/15", "04/04/16")
title=c("BURGLARY OF MOTOR VEHICLE", "BURGLARY OF MOTOR VEHICLE", "THEFT", "BURGLARY OF MOTOR VEHICLE", "THEFT")
acs=c("", "", "", "", "")
drug=c("", "", "", "", "")
pl=c("", "", "", "", "")
finding=c("DISMISS BY PROS", "", "DISMISS BY PROS", "", "DISMISS BY PROS")
tp=c("F", "F", "M", "F", "M")
lvl=c("9", "9", "A", "9", "A")
pn=c("N", "N", "N", "N", "N")
sentence_date=c("", "", "04/04/2016", "", "04/04/2016")

df <- data.frame(charge, section, date, title, acs, drug, finding, tp, lvl, pn, sentence_date,stringsAsFactors=FALSE)

The goal here is to remove every charge that is followed by an amended charge. The following dataset illustrates the desired result:

charge=c("Count 1", "Amended", "Amended")
section=c("21-5807(a)(3)", "21-5801(b)(4)", "21-5801(b)(4)")
date=c("09/13/15", "04/04/16", "04/04/16")
title=c("BURGLARY OF MOTOR VEHICLE", "THEFT", "THEFT")
acs=c("", "", "")
drug=c("", "", "")
pl=c("", "", "")
finding=c("DISMISS BY PROS", "DISMISS BY PROS", "DISMISS BY PROS")
tp=c("F", "M", "M")
lvl=c("9", "A", "A")
pn=c("N", "N", "N")
sentence_date=c("", "04/04/2016", "04/04/2016")

df <- data.frame(charge, section, date, title, acs, drug, finding, tp, lvl, pn, sentence_date,stringsAsFactors=FALSE)

One way to achieve this new dataset is to identify all charges that are followed by an amended charge and extract all charges except these original charges.

First, the positions of the updated charges are identified. The following expression identifies the updated charges as TRUE and all other charges as FALSE.

c(df$charge=="Amended")

Second, the positions of the updated charges are used to identify the positions of the original charges that were amended. This is accomplished by recognizing a pattern in the charges themselves. Updated charges in this dataset are always preceded by their original charge. Since the updated charges were identified as TRUE (and the non-updated charges identified as FALSE), shifting these values by one position identifies the original charges.

c(c(df$charge=="Amended")[-1], c(df$charge=="Amended")[1])

Finally, the positions of the original charges (i.e., the charges followed by the amended ones) are used to extract the desired dataset. The desired dataset consists of every charge not followed by an updated charge.

df[!c(c(df$charge=="Amended")[-1], c(df$charge=="Amended")[1]),]
Advertisement