Я пытаюсь сохранить первую строку серии, используя функцию сопоставления с R, а затем применяю ее к исходному фрейму данных, чтобы собрать только строку, в которой было выполнено первое условие. Я собираюсь выполнить вычисление и логический тест для этих дублированных значений в новых столбцах, но после получения правильного индекса совпадений и применения их к исходному фрейму данных я получаю полностью перепутанный результат, а не первое значение, которое я должен получить как содержится в соответствующей строке.
# 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),]
Вот пример 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")
Помощь очень ценится.