0

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
dario
  • 6,415
  • 2
  • 12
  • 26
signe
  • 37
  • 4
  • Hi Signe Kilskar. Can you show us what you already tried? Also please add a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). That way you can help others to help you! – dario Feb 21 '20 at 10:25

1 Answers1

0

Using tidyverse:

data %>% group_by(Date) %>% mutate(value = ifelse(Return > mean(Return), 1, 0))
John
  • 131
  • 5