Here's a data.table
approach
library(data.table)
# Mimic your dataset
dat = data.frame(`Claim Number` = c("1-12835", "1-12835_2",
"1-12835_3", "1-12835_4", "2", "3", "4", "5", "6-15302",
"6-15302_2", "7", "8", "9-16186", "9-16186_2"))
# Set the data.frame to data.table
setDT(dat)
# Get the "parent" claim number by removing any characters after the underscore
dat[, parent_claim_number := gsub("_.*", "", Claim.Number)]
# Add an indicator for any parent claim numbers with "sub" claims
dat[, has_sub_claim := any(grepl("_", Claim.Number)), by = .(parent_claim_number)]
Result is:
Claim.Number parent_claim_number has_sub_claim
1: 1-12835 1-12835 TRUE
2: 1-12835_2 1-12835 TRUE
3: 1-12835_3 1-12835 TRUE
4: 1-12835_4 1-12835 TRUE
5: 2 2 FALSE
6: 3 3 FALSE
7: 4 4 FALSE
8: 5 5 FALSE
9: 6-15302 6-15302 TRUE
10: 6-15302_2 6-15302 TRUE
11: 7 7 FALSE
12: 8 8 FALSE
13: 9-16186 9-16186 TRUE
14: 9-16186_2 9-16186 TRUE
If you want claims with a sub claim, you can do:
dat[has_sub_claim == TRUE]
If you want only the sub-claims without the parent claim, you can do:
dat[has_sub_claim == TRUE & grepl("_", Claim.Number)]