I have the following dataset about an investment returns:
df <- structure(list(Date = structure(c(1620604800, 1620604800, 1620604800,
1620604800, 1620604800, 1627257600, 1627257600), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), Client = c(56203, 56203, 56203, 56203,
56203, 56203, 56203), FundName = c("SAFRA KEPLER EQUITY HEDGE FI MULTIMERCADO",
"SAFRA KEPLER FI MULTIMERCADO", "SAFRA S&P SPECIAL FIC MULTIMERCADO",
"SAFRA AÇÕES LIVRE FIC AÇÕES", "SAFRA CONSUMO AMERICANO FIC AÇÕES BDR NÍVEL I",
"SAFRA S&P SPECIAL FIC MULTIMERCADO", "SAFRA CONSUMO AMERICANO FIC AÇÕES BDR NÍVEL I"
), Nature = c("Aplicação", "Aplicação", "Aplicação", "Aplicação",
"Aplicação", "Resgate", "Resgate"), Quantity = c(145.927569,
62.684383, 16.964545, 57.852278, 14.900635, 0, 0), Value = c(21240,
12120, 8760, 9120, 8760, -9157.45, -1125), Saldo = c(21240, 12120,
8760, 9120, 8760, 55.60576570794, 9198.89221970512), FundCode = c(25079578000106,
21144577000147, 19107923000175, 32666326000149, 19436818000180,
19107923000175, 19436818000180), NAVInitialDate = c(145.551660000114,
193.349593000021, 521.980031999759, 157.489282999886, 639.441026999615,
543.077092000283, 692.849144999869), NAVYesterday = c(147.526190999895,
196.017907999922, 531.332205999643, 166.330892999889, 675.641246000305,
531.332205999643, 675.641246000305)), row.names = c(NA, -7L), class = c("tbl_df",
"tbl", "data.frame"))
df$NAVInitialDate = df$Value / df$Quantity
The 'NAVInitialDate' column is result of "Value/Quantity" for each row. What I want to say to R is: "if in a Row we have Quantity = 0, then 'NAVInitialDate' = ABS(Value) divided by the SUM of Quantity for all rows that have the same FundName as the Fundname in the row where the Quantity is equal to 0"
The result should be, for Row 6 > NavInitialDate = 539,793 ; for Row 7 > NavInitialDate = 75,50
How can I do this? I guess it needs 3 conditions maybe
Aucun commentaire:
Enregistrer un commentaire