Ich bin ein Anfänger in data.table
und suchte nach Join und dann mutieren Spalten. Ich habe data.table join then add columns to existing data.frame without re-copy Thread gefunden, aber ich konnte nicht weiter fortfahren.Join dann mutieren mit data.table ohne Zwischentabelle
Bitte beachten Sie, dass ich in der Lage bin, was ich mit dplyr
tun möchte, aber es ist nicht machbar, diesen Code auf die tatsächlichen Daten aufgrund der Größe zu führen. Außerdem kann ich aus dem genannten Grund keine Zwischentabellen erstellen.
Hier sind meine Daten und Lösung dplyr
Eingang
DFI = structure(list(PO_ID = c("P1234", "P1234", "P1234", "P1234",
"P1234", "P1234", "P2345", "P2345", "P3456", "P4567"), SO_ID = c("S1",
"S1", "S1", "S2", "S2", "S2", "S3", "S4", "S7", "S10"), F_Year = c(2012,
2012, 2012, 2013, 2013, 2013, 2011, 2011, 2014, 2015), Product_ID = c("385X",
"385X", "385X", "450X", "450X", "900X", "3700", "3700", "A11U",
"2700"), Revenue = c(1, 2, 3, 34, 34, 6, 7, 88, 9, 100), Quantity = c(1,
2, 3, 8, 8, 6, 7, 8, 9, 40), Location1 = c("MA", "NY", "WA",
"NY", "WA", "NY", "IL", "IL", "MN", "CA")), .Names = c("PO_ID",
"SO_ID", "F_Year", "Product_ID", "Revenue", "Quantity", "Location1"
), row.names = c(NA, 10L), class = "data.frame")
Look Up Table
DF_Lookup = structure(list(PO_ID = c("P1234", "P1234", "P1234", "P2345",
"P2345", "P3456", "P4567"), SO_ID = c("S1", "S2", "S2", "S3",
"S4", "S7", "S10"), F_Year = c(2012, 2013, 2013, 2011, 2011,
2014, 2015), Product_ID = c("385X", "450X", "900X", "3700", "3700",
"A11U", "2700"), Revenue = c(50, 70, 35, 100, -50, 50, 100),
Quantity = c(3, 20, 20, 20, -10, 20, 40)), .Names = c("PO_ID",
"SO_ID", "F_Year", "Product_ID", "Revenue", "Quantity"), row.names = c(NA,
7L), class = "data.frame")
Ausgabe
mitDFO = structure(list(PO_ID = c("P1234", "P1234", "P1234", "P1234",
"P1234", "P1234", "P2345", "P2345", "P3456", "P4567"), SO_ID = c("S1",
"S1", "S1", "S2", "S2", "S2", "S3", "S4", "S7", "S10"), F_Year = c(2012,
2012, 2012, 2013, 2013, 2013, 2011, 2011, 2014, 2015), Product_ID = c("385X",
"385X", "385X", "450X", "450X", "900X", "3700", "3700", "A11U",
"2700"), Revenue = c(16.6666666666667, 16.6666666666667, 16.6666666666667,
35, 35, 35, 100, -50, 50, 100), Quantity = c(1, 1, 1, 10, 10,
20, 20, -10, 20, 40), Location1 = c("MA", "NY", "WA", "NY", "WA",
"NY", "IL", "IL", "MN", "CA")), .Names = c("PO_ID", "SO_ID",
"F_Year", "Product_ID", "Revenue", "Quantity", "Location1"), row.names = c(NA,
10L), class = "data.frame")
Hier ist mein Code dplyr
mit Ich bin mit zwei Bibliotheken hier: dplyr
und compare
verließ ich bin mit schließen, um neue Einträge aus der Look-Up-Tabelle in DFI
hinzufügen. Dann teile ich den Umsatz und die Spalte basierend auf der Anzahl der Zeilen in einer Gruppe. Dies ist, weil ich Inflation von Zahlen verhindern will, wenn sie gruppiert werden.
DF_Generated <- DFI %>%
dplyr::left_join(DF_Lookup,by = c("PO_ID", "SO_ID", "F_Year", "Product_ID")) %>%
dplyr::group_by(PO_ID, SO_ID, F_Year, Product_ID) %>%
dplyr::mutate(Count = n()) %>%
dplyr::ungroup()%>%
dplyr::mutate(Revenue = Revenue.y/Count, Quantity = Quantity.y/Count) %>%
dplyr::select(PO_ID:Product_ID,Location1,Revenue,Quantity)
Hier ist, wie die Ausgangsspiele:
compare(DF_Generated,DFO,allowAll = TRUE)
TRUE
ich aufrichtig schätzen würde jede mögliche Hilfe.
Ok. Alles was ich mache ist ein linker Join und dann mutieren Spalten im Ergebnis. Lassen Sie mich dennoch ein paar Zeilen schreiben. Ich habe die Logik hinzugefügt. – watchtower