I am creating a new variable mydata_weighted.equity
for my dataset using the following code:
mydata_weighted.equity <- ddply(mydata, .(TrackerID), function(x)
data.frame(weighted.equity = weighted.mean(x$Equity_dummy,
x$Financiers_Unit_Share_Dollar)))
This works.
Now, I want to do the same again, but include only those observations for which a special condition is met. Namely, that the value for the variable signatory
is >0.
I bet that there is a way like
mydata_weighted.equity <- ddply(mydata, .(TrackerID), function(x)
data.frame(weighted.equity = weighted.mean(x$Equity_dummy,
x$Financiers_Unit_Share_Dollar), if signatory > 0))
but I just don't figure out how to code it.
Desperatly looking for help as the deadline for my project is getting closer...
Try this:
library(tidyverse)
mydata_weighted.equity <- ddply(mydata, .(TrackerID), function(x){
x %>% filter(signatory>0) %>% summarise(weighted.equity = weighted.mean(Equity_dummy, Financiers_Unit_Share_Dollar)) %>% data.frame()
It will filter the data then calculate what you want!