I'm trying to retaining the first row of a series using the match feature with R and then applying it to the original data frame so that I only gather the row where the first condition was met. I'm going to run a calculation and logical test against these duplicated values in new columns but after getting the index of matches correct and then applying them to the original data frame I'm getting a completely mixed up result rather than the first value I should get as contained in its corresponding row.
# Extract first row's value
DT1 <- InsiderList3[!duplicated(`Insider CIK`), ]
# Construct Index for Each Grouping (Insider CIK) most recent reported transaction date
index2 <- match(as.character(InsiderList3$`Insider CIK`),as.character(DT1$`Insider CIK`))
dt3 <- InsiderList3[as.numeric(index2),]
Here is a sample dput:
dput(head(InsiderList3[c('Insider CIK', 'Transaction Date', 'Issuer')], 75))
structure(list(`Insider CIK` = c("0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001008134", "0001008134",
"0001008134", "0001008134", "0001008134", "0001009891", "0001012859",
"0001012859", "0001012859", "0001012859"), `Transaction Date` = structure(c(18358,
18358, 18101, 18065, 18065, 18039, 17729, 17700, 17674, 17674,
17345, 17345, 17326, 17014, 17014, 17014, 17014, 17014, 17014,
17001, 16964, 16964, 16598, 16590, 16582, 16582, 16409, 16288,
16288, 16245, 16245, 16217, 16161, 16072, 16052, 15967, 15880,
15869, 15771, 15710, 15710, 15687, 15603, 15523, 15354, 15354,
15030, 14979, 14840, 14049, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 18358, 18358,
18358, 18261), class = "Date"), Issuer = c("TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"SANDRIDGE ENERGY INC", "SANDRIDGE ENERGY INC", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "Seventy Seven Energy Inc.",
"Seventy Seven Energy Inc.", "Seventy Seven Energy Inc.", "Seventy Seven Energy Inc.",
"Seventy Seven Energy Inc.", "Seventy Seven Energy Inc.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "Seventy Seven Energy Inc.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"Seventy Seven Energy Inc.", "Seventy Seven Energy Inc.", "Seventy Seven Energy Inc.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP",
"CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP", "TRANSATLANTIC PETROLEUM LTD.",
"CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP",
"CHESAPEAKE ENERGY CORP", "TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "TRANSATLANTIC PETROLEUM LTD.",
"TRANSATLANTIC PETROLEUM LTD.", "QUEST RESOURCE CORP", "QUEST RESOURCE CORP",
"CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP",
"CHESAPEAKE ENERGY CORP", "CHESAPEAKE ENERGY CORP", "TRANSATLANTIC PETROLEUM LTD.",
"CHESAPEAKE ENERGY CORP", "Seventy Seven Energy Inc.", "CHESAPEAKE OILFIELD OPERATING LLC",
"TRANSATLANTIC PETROLEUM LTD.", "QUEST RESOURCE CORP", "CHESAPEAKE ENERGY CORP",
"CHESAPEAKE ENERGY CORP", "CVR ENERGY INC", "CHESAPEAKE ENERGY CORP",
"SANDRIDGE ENERGY INC", "TRANSATLANTIC PETROLEUM LTD.", "Seventy Seven Energy Inc.",
"CHESAPEAKE ENERGY CORP", NA, "NATIONAL HEALTHCARE CORP", "NATIONAL HEALTHCARE CORP",
"NATIONAL HEALTHCARE CORP", "NATIONAL HEALTHCARE CORP")), row.names = c(NA,
75L), class = "data.frame")
Help is much appreciated.