I have troubles putting my question into words (hence the weird title) but :
I want to create a new column Earnings, that will take the value of the Price
of the Date
that matches the Last trading day
. Like this :
For the first row, the last trading day is 2014-02-17
, so I check in the Date column, and in the 5th row, the Date is equal to 2014-02-17
. So I take the price of the 5th row which is 235 and assign it to all rows that have 2014-02-17
as the Last trading day
.
Price Date `Last trading day` Earnings
<dbl> <date> <date> <dbl>
224. 2013-01-02 2014-02-17 235
224. 2013-01-02 2014-02-17 235
224. 2013-01-02 2014-02-17 235
224. 2013-01-02 2014-04-19 260
235. 2014-02-17 2014-04-19 260
260. 2014-04-19 2014-06-17 253
I tried this, but it doesn't work :
library(dplyr)
library(plyr)
df<-data %>%
group_by(`Last trading day`) %>%
mutate(Earnings = if_else(data$Date==data$`Last trading day`, Price, NA_real_))
Thanks a lot for your help.