Reputation: 37
I am fairly new to R studio.. I am trying to divide stocks into classes based on their performance relative to the average returns of all the stocks in my sample. I have daily stock data for several stocks, and the data is stacked. I would like to, for each day, give the stock a value of 1 if the return is higher than the average stock returns in the sample, and 0 otherwise. How can i do that?
My dataset looks something like this
Date SecurityID Return
01.01.01 1 0.02
02.01.01 1 -0.005
03.01.01 1 0.01
...
01.01.01 10 0.1
02.01.02 10 0.005
03.01.01 10 0.01
Upvotes: 0
Views: 66
Reputation: 131
Using tidyverse:
data %>% group_by(Date) %>% mutate(value = ifelse(Return > mean(Return), 1, 0))
Upvotes: 0