Reputation: 11
I have the following data:
library(tidyverse)
df <- tribble(~SwapID, ~SwapLegID, ~LegPayerID, ~ CurrencyID, ~NotionalAmount,
'AB-D-0001', 1, 'AB', 'CAD', 250000000,
'AB-D-0001', 2, 'BMO', 'CAD', 250000000,
'AB-D-0002', 1, 'AB', 'CAD', 250000000,
'AB-D-0002', 2, 'RBC', 'CAD', 250000000,
'AB-D-0004', 1, 'AB', 'CAD', 250000000,
'AB-D-0004', 2, 'TD', 'USD', 250000000,
'AB-D-0005', 1, 'BMO', 'USD', 416666668,
'AB-D-0005', 2, 'AB', 'CAD', 548750002)
I need to find a way to divide "NotionalAmount" with each other for each respective "SwapID" and "SwapLegID".
Therefore, for "SwapID" AB-D-0005, I want to divide 548750002 by 416666668 to get 1.317 and assign this data element a new column.
FXrateSwap<-SwapLegs%>%
arrange(SwapID,SwapLegID)%>%
select(SwapID,SwapLegID,LegPayerID,CurrencyID,NotionalAmount)
Upvotes: 1
Views: 43
Reputation: 3639
Try this
library(tidyverse)
df %>%
group_by(SwapID) %>%
mutate(ratio = NotionalAmount[SwapLegID == 2]/NotionalAmount[SwapLegID == 1])
Upvotes: 0
Reputation: 769
Though you should consider the above concerns, technically this is what I think you'd want.
want = FXrateSwap %>%
group_by(SwapID) %>%
summarize(ratio_want = sum(NotionalAmount[SwapLegID==1])/sum(NotionalAmount)
Upvotes: 1