Вот подход с использованием data.table
:
library(data.table)
x[C != "trash", `:=`(B, C)][, dcast(.SD, rowid(A) ~ A, value.var = "B")]
## A Amount Date Name
## 1: 1 0.01 20/05/31 XYZ
## 2: 2 0.02 20/06/30 ABC
## 3: 3 -0.03 20/07/29 KLM
Вот «x»:
x <- structure(list(A = c("Name", "Date", "Amount", "Name", "Date",
"Amount", "Name", "Date", "Amount"), B = c("XYZ", "20/05/31",
"trash", "ABC", "20/06/30", "trash", "KLM", "20/07/29", "trash"
), C = c("trash", "trash", "0.01", "trash", "trash", "0.02",
"trash", "trash", "-0.03")), row.names = c(NA,
9L), class = c("data.table", "data.frame"))